Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Bitmap contents of one ImageView to anoher

This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.

Here is some code that doesn't work.

ImageView ivSrc = (ImageView) findViewById(R.id.photo);

ivSrc.setDrawingCacheEnabled(true);
    Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException

    Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null

    View vSrc = (View) ivSrc.getParent();
    vSrc.setDrawingCacheEnabled(true);
    Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache());  //black bitmap 

//To test the bitmaps:

     ImageView ivDest = (ImageView) findViewById(R.id.photo2);
      ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above

I have to going about this wrong because doing a copy should be so easy. TIA

like image 910
Jim Avatar asked Oct 21 '10 14:10

Jim


1 Answers

Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?

The way I'd do it:

Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);

Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.

like image 105
Reuben Scratton Avatar answered Oct 17 '22 23:10

Reuben Scratton