Pass Bitmap Data Between Activities in Android
Start New Activity from Current ActivityIntent anotherIntent = new Intent(this, anotherActivity.class);Start New Acticity from Current Activity With Passing Required Data
startActivity(anotherIntent);
finish();
Intent anotherIntent = new Intent(this, anotherActivity.class);putExtra() method is used to send extra data from one activity to another.
anotherIntent.putExtra("key", "value");
startActivity(anotherIntent);
finish();
Extract Data In Other Activity
data = getIntent().getStringExtra("key");getIntent() method returns the intent that started this activity.
getStringExtra() retrieves extended data from the intent.
Now, it turns out that it’s not possible to pass Bitmap as extended data. It needs to be converted to byteArray.
Pass Bitamp as Extended Data
ByteArrayOutputStream bStream = new ByteArrayOutputStream();Retrieve Bitmap in Other Activity
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
byte[] byteArray = bStream.toByteArray();
Intent anotherIntent = new Intent(this, anotherActivity.class);
anotherIntent.putExtra("image", byteArray);
startActivity(anotherIntent);
finish();
Bitmap bmp;
byte[] byteArray = getIntent().getByteArrayExtra("image");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
No comments:
Post a Comment