gpt4 book ai didi

android - 图片从SD卡到byte[]

转载 作者:行者123 更新时间:2023-11-29 16:09:05 28 4
gpt4 key购买 nike

如何从android模拟器SD卡获取图片的byte[]?

这是我当前的代码:

File f = new File("/mnt/sdcard/test.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.iv);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());

我想要的是图像的字节格式,我该怎么做?

最佳答案

创建一个ByteArrayOutputStream,然后调用Bitmapcompress()方法,这样bitmap的内容就会传入ByteArrayOutputStream 并转换为 byte[]

public byte[] bitmapToByteArray(Bitmap b)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}

申请,

Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());

byte[] bitmapBytes = bitmapToByteArray(bmp);

关于android - 图片从SD卡到byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15059584/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com