2013-02-25 52 views
1

我怎样才能从android模拟器SD卡的图像字节[]?图像从SD卡到字节[]

这里是我当前的代码:

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

我要的是像我怎么能做到这一点的字节格式?

回答

4

创建ByteArrayOutputStream然后调用方法Bitmap这样的位图的内容将在ByteArrayOutputStream传递compress(),并转换为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); 
0

您可以将位图为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();