Activity之间传递Bitmap方式

这篇博客介绍了在Android中如何在Activity之间传递Bitmap,包括使用Bundle的putParcelable方法和putByteArray配合压缩图片的方式,以及将Bitmap存入SDCard后再进行读取的方法。文章提到了通过Bundle传递图片的大小限制以及压缩图片以避免OOM的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 使用Bundle和intent。(传递图片有大小限制,否则会导致OOM)(个人推荐用这种,限制传递图片大小)

(1). 使用Bundle的putParcelable方法:

Bundle bundle = new Bundle();
Intent intent = new Intent();
intent.putExtra("bitmap", bitmap);
bundle.putParcelable("bitmap", bitmap);

传递图片较小(亲测,可传递小于88K的图片)


(2).使用Bundle的putByteArray,先压缩图片:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, baos);
byte[] bitmapByte = baos.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("bitmapByte", bitmapByte);
//取图片的时候
byte[] bitmapByte = bundle.getByteArray("bitmapByte");
这个也有限制:最大可分享bitmapByte.length <= 517654, 大于等于523523的图片传递不成功,区间值中其它的没有进一步实测~

可参考:quality为85(压缩比25%)时:可分享的最大图片是1.2多一点

压缩比越大,可传递图片越大,且不会失像素~

2. 存到sdcard,再读取。(IO存取过程消耗较大)

(1).图片存到sdcard:

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    picPath = Environment.getExternalStorageDirectory() + File.separator + getBaseContext().getPackageName() + File.separator + "sharePics";
    String timestamp = String.valueOf(new Date().getTime());
    picName = "flight_" + timestamp;
    writeToSDCard(picPath, picName, bytes);
}
public static void writeToSDCard(String path, String filename, byte[] fileContent) {
    FileOutputStream outputStream = null;
    createDir(path);
    File file = new File(path, filename);
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(fileContent);
    } catch (IOException e) {
        QLog.e("writeToSDCard", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                QLog.e("writeToSDCard", e);
            }
        }
    }
}

public static void createDir(String path) {
    File file = new File(path);
    if (!file.exists()) {
        file.mkdirs();
    }
}

(2).读取图片:

String picPath = bundle.getString("picPath");
String picName = bundle.getString("picName");
byte[] picByte = toByteArray(picPath, picName);
Bitmap bitmapFromFile = bytes2Bimap(picByte);

public static byte[] toByteArray(String path, String filename) {
    File f = new File(path, filename);
    if (!f.exists()) {

    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
    BufferedInputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(f));
        int buf_size = 1024;
        byte[] buffer = new byte[buf_size];
        int len = 0;
        while (-1 != (len = in.read(buffer, 0, buf_size))) {
            bos.write(buffer, 0, len);
        }
        return bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

public Bitmap bytes2Bimap(byte[] b) {
    if (b.length != 0) {
        return BitmapFactory.decodeByteArray(b, 0, b.length);
    } else {
        return null;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值