Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert frame layout into image and save it [closed]

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.

like image 427
Akshay Avatar asked Feb 12 '14 10:02

Akshay


People also ask

Why FrameLayout hold one view?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

How to add image resource in android studio?

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.


1 Answers

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" />
like image 83
GhoRiser Avatar answered Oct 11 '22 22:10

GhoRiser