2013-12-12 68 views
3

我想將圖像以Base64格式字符串發佈到Web服務器。我需要從網絡服務器取回它並以位圖格式解碼它,以便我可以在listview中顯示圖像。但是在解碼時出現錯誤。 請看看我在這裏發佈的代碼..如何解碼來自json對象的圖像?

編碼:

ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, byteArrayBitmapStream); 
byte[] b = byteArrayBitmapStream.toByteArray(); 
String encodedImage = MyBase64.encode(b); 

解碼:

byte[] byteArray = Base64.decode(jsonObject.getString("imageData"), Base64.DEFAULT) ; 
System.out.println("byte[]:"+byteArray); 
Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

這裏是日誌:[錯誤的logcat顯示]

12-12 06:09:12.243: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.243: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
12-12 06:09:12.313: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.313: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
12-12 06:09:12.393: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.393: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
12-12 06:09:12.473: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.473: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
12-12 06:09:12.533: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.533: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
12-12 06:09:12.613: E/BitmapFactory(1037): Unable to decode stream: java.io.FileNotFoundException: /[email protected]: open failed: ENOENT (No such file or directory) 
12-12 06:09:12.613: I/System.out(1037): resolveUri failed on bad bitmap uri: [email protected] 
+0

此鏈接是有用的http://stackoverflow.com/questions/19767615/how-to-display-image-byte-array-from-json-into-imageview-factory-returns-nu –

+0

你在解碼或編碼時遇到這個錯誤? –

+0

@ M-WaJeEh解碼時出現此錯誤,請幫我解決。 – user2879697

回答

0

嘿你想解碼圖像從json對象

則u數據庫和訪問的第一家店圖像URL化JSON

則比從URL獲取的位圖圖像

使用下面的代碼

try { 
     URL url = new URL(url); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    }