从SD卡到字节[]的图像

时间:2013-02-25 03:23:00

标签: android image-processing bytearray

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

这是我目前的代码:

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

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

2 个答案:

答案 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();