user3465277
user3465277

Reputation: 219

Image and buildDrawingCache

I have imageView1 and i want to turn it into bitmap and send to previous activity, so my code is:

    imageView1.buildDrawingCache();
       Bitmap bm=imageView1.getDrawingCache();
        int bytes =  bm.getWidth()*bm.getHeight()*4;      
     ByteBuffer buffer = ByteBuffer.allocate(bytes); 
      bm.copyPixelsToBuffer(buffer); 
      final byte[] array = buffer.array(); 

But i am getting :

04-25 17:45:55.959: E/AndroidRuntime(7066): Caused by: java.lang.NullPointerException

A dont know if i can use drawingCache this way.

Upvotes: 2

Views: 238

Answers (1)

Phantômaxx
Phantômaxx

Reputation: 38098

Assuming you have an activity called ChildActivity, in which you use imageView1,
instead of declaring it like:

ImageView imageView1;

Do:

protected static ImageView imageView1 = null;

This has to be done at the highest scope level, in the class declarations section, in order to be visible outside any method.

Then, assuming you have an activity called ParentActivity, in which you want to use imageView1's graphics,
you can refer the imageView1 control by prepending the activity name to it:

Bitmap bmp = ((BitmapDrawable) ChildActivity.imageView1.getDrawable()).getBitmap();

The key feature is: ChildActivity.imageView1

I referred the ImageView in the other activity.
And I didn't have to mess with passing a bitmap between activities.

It's safer and (I guess) faster than what you were trying to do.

Upvotes: 1

Related Questions