views:

22

answers:

1

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

+1  A: 

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.

Reuben Scratton
Works like a CHARM!!!! Thanks!
Jim
No worries. Do mark it as the answer though, if it helped. Not that I want the reputation points or anything like that, oh no. :)
Reuben Scratton