2012-03-20 62 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();