19

can any one please help me to know how to capture contents of a FrameLayout into an image and save it to internal or external storage.

how can convert any view to image

Akshay
  • 6,029
  • 7
  • 40
  • 59

2 Answers2

39

try this to convert a view (framelayout) into a bitmap:

public Bitmap viewToBitmap(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

then, save your bitmap into a file:

try {
        FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

don't forget to set the permission of writing storage into your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
bjb568
  • 11,089
  • 11
  • 50
  • 71
GhoRiser
  • 686
  • 4
  • 3
  • Be sure to close the stream in case of an exception; I'd recommend adding a `finally` block to close the stream and recycle the bitmap. – Paul Lammertsma Aug 17 '14 at 17:08
  • doing same method the image get saved but image is all black any idea what mistake did i make.. – user2273146 Nov 25 '15 at 10:33
  • So I did this with a framelayout but the width and height was not able to be obtained, using a `ViewTreeObserver` I was able to get the info after it rendered from from inside onCreate. This worked great for thank you. for more info see http://stackoverflow.com/a/8171014/1815624 – CrandellWS Jan 30 '16 at 09:55
  • You could make this more efficient by calling `view.getDrawingCache(true/false)`, because the view drawing may already be cached. Then, if `getDrawingCache()` returns null, call `Bitmap.createBitmap()` as you have it. For more info see the docs: https://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean) – w3bshark Oct 07 '16 at 02:12
  • @GhoRiser I trying with framelayout but all the time I am getting framelayout width as the screen size how can I resolve it / – PriyankaChauhan Nov 02 '16 at 10:17
9

try this...

public static void saveFrameLayout(FrameLayout frameLayout, String path) {
    frameLayout.setDrawingCacheEnabled(true);
    frameLayout.buildDrawingCache();
    Bitmap cache = frameLayout.getDrawingCache();
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(path);
        cache.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        frameLayout.destroyDrawingCache();
    }
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Thanks... for making efforts.. – Akshay Feb 12 '14 at 11:23
  • @Gopal Gopi I trying with framelayout but all the time I am getting framelayout width as the screen size how can I resolve it – PriyankaChauhan Nov 02 '16 at 10:21
  • @pcpriyanka if your framelayout dimensions are as match_parent, You will get screen size only – Gopal Gopi Nov 02 '16 at 11:43
  • @GopalGopi then What is should be ? – PriyankaChauhan Nov 02 '16 at 11:54
  • right now it is fill_parent – PriyankaChauhan Nov 02 '16 at 11:54
  • Hi @PriyankaChauhan Its very late but it may helpfull for someone. I got what you want. I found solution. You need to use "frameLayout.getDrawingCache(true);" instead of "frameLayout.getDrawingCache();". Here we are sending true for auto scaling that will provide us scaled image not equivalent to screen width. For more details refer to this https://developer.android.com/reference/android/view/View.html#getDrawingCache() – Rahul Sharma Feb 15 '17 at 07:58
  • @GopalGopi Your code is working properly the view is saved as image but when i share the saved image in whatsapp then i am not able to share it. It's showing me The file format is not supported. But i am able to open it in device. Can you check this – Ketan Ramani Apr 04 '19 at 15:49