10

I have posted same question but it's in near to my problem that's why i have posted it second time

Hi i want to capture image from RelativeLayout, for that i have used below code

   captureRelativeLayout.setDrawingCacheEnabled(true);
   bitmap = captureRelativeLayout.getDrawingCache(true).copy(
                Config.ARGB_8888, false);

the problem is that when i start activity and get image from that view at that time it will work fine, but if i use it second time, the image is not being refreshed, means that previous bitmap is every time i getting.

Now if i close my activity and agian start it then i will get updated image but again not in second time :( for more information look at the can't share image properly android

Community
  • 1
  • 1
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

6 Answers6

8

Here are two ways to convert a view to a bitmap:

Use Drawing Cache:

RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);

I've had some issue with the drawing cache method when the view is very large (for example, a TextView in a ScrollView that goes far off the visable screen). In that case, using the next method would be better.

Use Canvas:

RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
    bgDrawable.draw(canvas);
} else {
    canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
7

You should destroy the drawing cache after copying it, so the cache will be built again next time you call getDrawingCache().

The code would look like this:

captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();

Or like this if you don't want to enable the flag:

captureRelativeLayout.buildDrawingCache(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();

Reference: https://groups.google.com/d/msg/android-developers/IkRXuMtOA5w/zlP6SKlfX-0J

huy.nguyen
  • 454
  • 2
  • 9
2

There is a Kotlin extension function in Android KTX:

val config: Bitmap.Config = Bitmap.Config.ARGB_8888
val bitmap = canvasView.drawToBitmap(config)
Rathi J
  • 849
  • 9
  • 16
1

finally i got solution from this view.getDrawingCache() only works once

i just forget to put

     captureRelativeLayout.setDrawingCacheEnabled(false);

i have done it by below code,

    try {
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        captureRelativeLayout.setDrawingCacheEnabled(true);
        File sdcard = Environment.getExternalStorageDirectory();
        File f = new File(sdcard, "temp.jpg");
        FileOutputStream out = null;
        out = new FileOutputStream(f);
        captureRelativeLayout.setDrawingCacheEnabled(true);
        bitmap = captureRelativeLayout.getDrawingCache(true).copy(
                Config.ARGB_8888, false);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.close();
        captureRelativeLayout.setDrawingCacheEnabled(false);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
        sharingIntent.setType("image/jpg");

        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
        startActivity(Intent.createChooser(sharingIntent, "Share Image"));
    } catch (Exception e) {
        e.printStackTrace();
    }
Community
  • 1
  • 1
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
1

As DrawingCache is depracated then in kotlin:

fun View.createBitmap(): Bitmap {
    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    Canvas(bitmap).apply {
        background?.draw(this) ?: this.drawColor(Color.WHITE)
        draw(this)
    }
    return bitmap
}
Renetik
  • 5,887
  • 1
  • 47
  • 66
0

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bitmap = view.getDrawingCache();

"bitmap" is the final bitmap..

dileep krishnan
  • 326
  • 4
  • 7