How to pass drawable between activities
Solution 1:
1) Passing in intent as extras
In the Activity A you decode your image and send it via intent:
- Using this method (extras) image is passed in 162 milliseconds time interval
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2) Saving image file and passing its reference to another activity
- WHY to do so? - taken from http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#
"The size limit is: keep it as small as possible. Definitely don't put a bitmap in there unless it is no larger than an icon (32x32 or whatever).
- In *Activity A* save the file (Internal Storage)
String fileName = "SomeName.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(b); //b is byte array
//(used if you have your picture downloaded
// from the *Web* or got it from the *devices camera*)
//otherwise this technique is useless
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
- Pass location as String to Activity B
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);
- In *Activity B* retrieve the file
Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");
- Make *drawable* out of the picture
File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());
- Apply it to the ImageView resource
someImageView.setBackgroundDrawable(d);
Solution 2:
You can tag each image (in the xml, or programmaticlly) with the image resource name (like "img1.png"),
then retrieve the image name using the getTag();
Then use getResources().getIdentifier(image name,"drawable", .getPackageName())
to get the drawable resource id.
And just pass the resource id through the intent -
intent.putExtra("img 1",resource id);
Lastly the result Activity can create the image from the resource using:
getResources().getDrawable(intent.getIntExtra("img 1",-1));
Solution 3:
Drawable
objects are not inherently serializable, so they cannot be passed directly in Intent
extras. You must find another way to serialize or persist the image data and retrieve it in the new Activity.
For example, if you are working with BitmapDrawable
instances, the underlying Bitmap
could be written out to a file and read back, or serialized into a byte array (if its small enough) and the byte array could be passed via extras of an Intent
.
HTH
Solution 4:
Much much much better not to pass (or serialize) Drawables
around among Activities
. Very likely your are getting the drawable out of a resource. Hence there's a resource ID. Pass that around instead, that's just an int. And re-hydrate the Drawable
in the other Activity
.
If the Drawable
is not coming from a resource, but it's built at runtime in memory ... well let's speak about it. @Devunwired has a nice suggestion in that case.