Convert Any view into image and save it [closed]
Asked Answered
F

2

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

Fussbudget answered 12/2, 2014 at 10:56 Comment(3)
You mean print screen ?Weymouth
NO. Convert layout into an image..Fussbudget
FrameLayout is a layout, image is an image. They are quite different. How can I convert an apple to an orange ?Weymouth
M
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" />
Monitory answered 12/2, 2014 at 11:4 Comment(5)
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.Sclerite
doing same method the image get saved but image is all black any idea what mistake did i make..Ariel
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 https://mcmap.net/q/145128/-getheight-returns-0-for-all-android-ui-objectsAlgid
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: developer.android.com/reference/android/view/…Ariadne
@Monitory I trying with framelayout but all the time I am getting framelayout width as the screen size how can I resolve it /Jaquez
S
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();
    }
}
Steatopygia answered 12/2, 2014 at 11:2 Comment(7)
Thanks... for making efforts..Fussbudget
@Gopal Gopi I trying with framelayout but all the time I am getting framelayout width as the screen size how can I resolve itJaquez
@pcpriyanka if your framelayout dimensions are as match_parent, You will get screen size onlySteatopygia
@GopalGopi then What is should be ?Jaquez
right now it is fill_parentJaquez
Hi @Jaquez 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 developer.android.com/reference/android/view/…Typewriting
@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 thisScolex

© 2022 - 2024 — McMap. All rights reserved.