如何从android模拟器SD卡中获取图像的byte []?
这是我目前的代码:
File f = new File("/mnt/sdcard/test.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.iv);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
我想要的是图像的字节格式我该怎么做?
答案 0 :(得分:4)
创建ByteArrayOutputStream
,然后调用compress()
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);
答案 1 :(得分:0)
您可以将Bitmap转换为byte [],如:
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Folder/" + filename + ".PNG";
Bitmap bmp = BitmapFactory.decodeFile(imageInSD);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();