6

Everything I tried with setDrawingCacheEnabled and getDrawingCache was not working. The system was making an image but it just looked black.

Other people on SO seemed to be having a similar problem but the answers seemed either too complicated or irrelevant to my situation. Here are some of the ones I looked at:

And here is my code:

    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = view.getDrawingCache();
    try {
        FileOutputStream stream = new FileOutputStream(getApplicationContext().getCacheDir() + "/image.jpg");
        bitmap.compress(CompressFormat.JPEG, 80, stream);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    view.setDrawingCacheEnabled(false);

I'm sharing my answer below in case anyone else makes the same mistake I did.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Working fine for me just follow this link code [convert view to bitmap](https://stackoverflow.com/a/48935539/7589424) – Sagar Jethva Feb 22 '18 at 20:28

3 Answers3

14

My problem was that my view was a TextView. The text on the TextView was black (naturally) and in the app the background looked white. However, I later recalled reading that a view's background is by default transparent so that whatever color is below shows through.

So I added android:background="@color/white" to the layout xml for the view and it worked. When I had been viewing the image before I had been looking at black text over a black background.

See the answer by @BraisGabin for an alternate way that does not require overdrawing the UI.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Be carefull with this answer. More info: http://developer.android.com/intl/es/tools/performance/debug-gpu-overdraw/index.html – Brais Gabin Nov 30 '15 at 15:38
  • Useless backgrounds are bad for performance. If you need a background in your UI there is no problem to use it. Just think that if you draw 3 backgrounds at the same pixel the only necessary one was the last one. – Brais Gabin Dec 01 '15 at 08:09
11

I just found a good option:

final boolean cachePreviousState = view.isDrawingCacheEnabled();
final int backgroundPreviousColor = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(0xfffafafa);
final Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheBackgroundColor(backgroundPreviousColor);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
view.setDrawingCacheEnabled(cachePreviousState);

Where 0xfffafafa is the desired background color.

StephenA
  • 61
  • 6
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
  • I haven't worked on this for a while, but I starred my question to come back to it in the future and test this out. For now +1. – Suragch Dec 02 '15 at 12:40
3

Used below code to get bitmap image for view it work fine.

 public Bitmap loadBitmapFromView(View v) {
         DisplayMetrics dm = getResources().getDisplayMetrics();
         v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, 
         View.MeasureSpec.EXACTLY),
         View.MeasureSpec.makeMeasureSpec(dm.heightPixels, 
         View.MeasureSpec.EXACTLY));
         v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
         Bitmap returnedBitmap = 
         Bitmap.createBitmap(v.getMeasuredWidth(),
         v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
         Canvas c = new Canvas(returnedBitmap);
         v.draw(c);

        return returnedBitmap;
}
Sagar Jethva
  • 986
  • 12
  • 26