2012-03-20 61 views
-1

这是将图像文件转换为字节数组的代码。从Android模拟器中读取图像

public String GetQRCode() throws FileNotFoundException, IOException { 
    /* 
    * In this function the first part shows how to convert an image file to 
    * byte array. The second part of the code shows how to change byte array 
    * back to a image. 
    */ 
    AssetManager mgr = mAppView.getContext().getAssets(); 
    InputStream in = mgr.open("www/Siemens_QR.jpg"); 
InputStreamReader isr = new InputStreamReader(in); 
    char[] buf = new char[20]; 
    isr.read(buf, 0, 20); 
    isr.close(); 
    // byte[] bytes = bos.toByteArray(); 
    String abc = buf.toString(); 
    return abc; 
} 

这里我将图像文件转换为字节数组。我能够做到这一点。但是,当尝试使用存储在模拟器中的路径(“sdcard/Download/Siemens_QR.jpg”)读取此映像文件时,我正在收到VM中止错误。请建议我读取存储在仿真器中的图像文件的正确路径。

+1

希望的方式,你会做它在模拟器与手机或平板电脑上的操作方式完全相同。如果是这样,问题不是'如何在模拟器中做到这一点?'但'怎么做?'。 – 2012-03-20 08:40:26

+0

你想从IDE模拟器运行这个...? – 2012-03-20 08:40:59

回答

0

如果你有jpg图片存储在SD卡上,然后获取文件的路径,并尝试将图像转换使用以下方法字节...

Bitmap bitmap = BitmapFactory.decodeFile(file path); 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos); 

byte[] byte_img_data = baos.toByteArray();