I have a View
and I want to convert it into an image in order to store it somewhere.
But how can I convert this View
to an image?
To import image resources into your project, do the following: Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import.
In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the drawable folder contains the different types of images used for the development of the application.
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.
Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);
Try this for take image of view and store in sd card..
View view = TextView.getRootView();
//You can use any view of your View instead of TextView
if (view != null)
{
System.out.println("view is not null.....");
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
try
{
if (bm != null)
{
String dir = Environment.getExternalStorageDirectory().toString();
System.out.println("bm is not null.....");
OutputStream fos = null;
File file = new File(dir,"sample.JPEG");
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();
}
}
catch(Exception e)
{
System.out.println("Error="+e);
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With