开发者

How to send a bitmap between two different activity classes in android

In our application we need to send a bitmap from one activity class to another activity after doing some image processing. We call methods in the first activity and then we want to show the output image in the second acti开发者_如何学Pythonvity. The two activity classes have different layout xml files. How can we do that?


the Bitmap is parceable as EboMike said , so in your first Activity , you can do this :

Intent intent = new Intent(this,SecondActivity.class);
intent.putExtras("MYBITMAP",yourImage);
startActivity(intent);

and in your SecondActivity , add this code :

Bitmap imageToDisplay = (Bitmap) this.getIntent().getExtras("MYBITMAP");
//and then you can display it in your imageView :)


A Bitmap is parcelable, so you can send it as an extra, BUT this is a bad idea if your bitmap is big - it might fail on older phones that don't have much RAM.

If you have really big Bitmaps, you should consider writing them to the internal storage as they are being transferred. This will also handle the case where a user temporarily switches to a different app (like an incoming phone call) and then comes back to your app, which has possibly been terminated at that point.


If the activities are in the same apk then the best way is to simply to use a static variable.

You will be processing the bitmap object (from a Canvas?)

class Globals {
    public static BitmapDrawable processedBitmapDrawable=null;
}

.... in process activity:

Bitmap processedBitmap = canvas.getBitmap();
Globals.processedBitmapDrawable = new BitmapDrawable(processedBitmap); 

...

in second acitity:

if (Globals.processedBitmapDrawable!=null) {
   imageView.setDrawable(Globals.processedBitmapDrawable);
}

it seems (and is) simple, but is the best way as it save processing/loading the bitmap multiple times.

You might choose to also use a SoftReference<Bitmaprawable> this allows garbage collection to clean up the reference if nessecary. though you may ned to reload/repocess if you need it again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜