2014-01-23 87 views
-1

所以我想爲我使用base64編碼的圖像取一個字符串,並將其變成我可以在ImageView中使用的圖像。編碼它的代碼是:base64中的編碼圖像回到圖像

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] b = baos.toByteArray(); 
image_str = Base64.encodeToString(b, Base64.DEFAULT); 

我假設它會將image_str轉換回byteArray,然後返回到位圖?

我不是很熟悉base64的功能,所以我想我會在這裏問我搜索,以便在相同的時間內完成更多的工作。

謝謝你在前進,

泰勒

編輯:我發現這段代碼,但圖像不顯示出來,並logcat的說,解碼返回false:

byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP); 
InputStream in = new ByteArrayInputStream(imageBytes); 
Bitmap b = BitmapFactory.decodeStream(in); 
+0

你會BASE64將字符串解碼爲字節,然後將字節轉換回位圖。 –

+0

我發現這個: byte [] imageBytes = Base64.decode(imageString,Base64.NO_WRAP); InputStream in = new ByteArrayInputStream(imageBytes); 位圖b = BitmapFactory.decodeStream(in); 但它不工作,它說在logcat解碼返回false,然後不顯示圖像,但我確實看到字符串在那裏,並正確 – TylerM

回答

2

你」 d首先將Base64編碼的字符串解碼爲字節:

byte[] decodedBytes = Base64.decode(image_str, Base64.DEFAULT); 

然後將字節回到JPG:

Bitmap bm = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); 
+0

這似乎並沒有工作。 Logcat說,解碼返回false,圖像沒有顯示出來。 – TylerM

+0

然後,你在某種程度上改變了編碼/解碼之間的字符串。這就是它的工作原理。 –

+0

嗯,好吧,我不認爲有任何東西被添加到字符串,但我會確保。有沒有在線轉換器,我可以檢查它?就像我可以在編碼字符串中複製/粘貼的位置一樣,它會返回圖像? – TylerM