2

I want to make Bitmap data from activity's output. Activity contains a fragment, and I can't modify it.

I don't want to activity to real screen and just want to make bitmap data. How can I do it.

2 Answers2

2

I assume you want something like a screenshot of your activity.

In order to get the root view of your activity (what you're currently seeing), do this:

View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);

and then you'll have a Bitmap of your activity, referenced by the bitmap variable.

Do what you want with that Bitmap, like saving it to a file.

Community
  • 1
  • 1
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
1

If you wan to capture the Activity's View hierarchy to a Bitmap, try this:

View view = findViewById(android.R.id.content);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
BladeCoder
  • 12,779
  • 3
  • 59
  • 51