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
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
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" />
finally
block to close the stream and recycle the bitmap. –
Sclerite 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-objects –
Algid 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 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();
}
}
© 2022 - 2024 — McMap. All rights reserved.
FrameLayout
is a layout, image is an image. They are quite different. How can I convert an apple to an orange ? – Weymouth