简体   繁体   中英

How to send image from one activity to another in kitkat android?

In first Activity:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
i.putExtra("image", bytes);
startActivity(i);

In second Activity:

byte[] byteArray = extras.getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

if (bmp != null) {         
    iv_1.setImageBitmap(bmp);
}

This is working for all devices and versions. But it is not working for Kitkat, why? How to solve the issue in kitkat?

Passing such a huge file through intent is not a good practice. It would slow down the process of launching new activity.

Try to make a static reference of the image and use it in the next activity. As soon as you are done, just make it null

使用Map<String, Bitmap>一个单例类,该类将保留您需要的所有图像,并通过意图仅发送其键名。

It is not a good for performance to pass Bitmaps from on activity to another.

Just try to save the bitmap in memory and send "Path" of bitmap to another activity and then just use BitmapFactory.decodeFile(pathName); method in another activity to get bitmap from path.

In your first activity convert imageview to bitmap

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

in second activity

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Its working on kitkat also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM