Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a View to a Drawable?

Tags:

android

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?

like image 278
James Avatar asked Apr 25 '11 08:04

James


People also ask

How do you insert a picture into drawable?

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.

How do I get to the drawable folder on Android?

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.

What is a drawable?

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.

Which function is used to load a drawable image resource?

Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);


1 Answers

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();
    }
}
like image 125
Niranj Patel Avatar answered Oct 10 '22 04:10

Niranj Patel