Skip to content Skip to sidebar Skip to footer

How Can I Convert A View To A Drawable?

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?

Solution 1:

Try this for take image of view and store in sd card..

Viewview= TextView.getRootView();
//You can use any view of your View instead of TextViewif (view != null)
{
    System.out.println("view is not null.....");
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmapbm= view.getDrawingCache();

    try
    {
        if (bm != null)
        {
            Stringdir= Environment.getExternalStorageDirectory().toString();
            System.out.println("bm is not null.....");
            OutputStreamfos=null;
            Filefile=newFile(dir,"sample.JPEG");
            fos = newFileOutputStream(file);
            BufferedOutputStreambos=newBufferedOutputStream(fos);
            bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
            bos.flush();
            bos.close();
        }
    }
    catch(Exception e)
    {
        System.out.println("Error="+e);
        e.printStackTrace();
    }
}

Solution 2:

  1. Enable drawing cache on the view:

    view.setDrawingCacheEnabled(true);
    
  2. Create a bitmap from the cache:

    bitmap = Bitmap.createBitmap(view.getDrawingCache());
  3. Save the bitmap wherever...

  4. Disable drawing cache:

    view.setDrawingCacheEnabled(false);
    

Post a Comment for "How Can I Convert A View To A Drawable?"