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.

1

3 Answers 3

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.

2
  • Be carefull with this answer. More info: developer.android.com/intl/es/tools/performance/… Commented Nov 30, 2015 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. Commented Dec 1, 2015 at 8: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.

1
  • 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
    Commented Dec 2, 2015 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;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.