[英]Convert base64 string to Image in Java
我有一個圖像通過JSON字符串發送給我。 我想將該字符串轉換為我的Android應用程序中的圖像,然后顯示該圖像。
JSON字符串如下所示:
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
注意:我用...截斷了字符串
我有一個函數(我認為)將字符串轉換為圖像。 我這樣做了嗎?
public Bitmap ConvertToImage(String image){
try{
InputStream stream = new ByteArrayInputStream(image.getBytes());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
return bitmap;
}
catch (Exception e) {
return null;
}
}
然后我嘗試在我的Android活動上顯示它
String image = jsonObject.getString("barcode_img");
Bitmap myBitmap = this.ConvertToImage(image);
ImageView cimg = (ImageView)findViewById(R.id.imageView1);
//Now try setting dynamic image
cimg.setImageBitmap(myBitmap);
但是,當我這樣做時,什么都沒有出現。 我沒有在logcat中得到任何錯誤。 我究竟做錯了什么?
謝謝
我擔心你只需解碼base64字符串來獲取圖像字節,所以在你的
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
字符串,你必須得到數據之后的data:image\\/png;base64,
,所以你只得到圖像字節然后解碼它們:
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
這是一個代碼,所以你了解它是如何工作的,但如果你收到一個JSON對象,它應該以正確的方式完成:
data
鍵下提取String。 image/png
開頭,因此您知道是png圖像。 base64
字符串,因此您知道必須解碼數據。 base64
字符串之后解碼數據以獲取圖像。 InputStream stream = new ByteArrayInputStream(image.getBytes());
應改為
InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT));
有關如何進行Base64解碼的詳細信息,請參閱http://developer.android.com/reference/android/util/Base64.html 。
免責聲明:我沒有檢查語法,但這是你應該怎么做。
以下是轉換Base64編碼的inputStream並將其寫入磁盤的工作代碼。 我花了一些時間讓它正常工作。 所以我希望這有助於其他開發人員。
public boolean writeImageToDisk(FileItem item, File imageFile) {
// clear error message
errorMessage = null;
FileOutputStream out = null;
boolean ret = false;
try {
// write thumbnail to output folder
out = createOutputStream(imageFile);
// Copy input stream to output stream
byte[] headerBytes = new byte[22];
InputStream imageStream = item.getInputStream();
imageStream.read(headerBytes);
String header = new String(headerBytes);
// System.out.println(header);
byte[] b = new byte[4 * 1024];
byte[] decoded;
int read = 0;
while ((read = imageStream.read(b)) != -1) {
// System.out.println();
if (Base64.isArrayByteBase64(b)) {
decoded = Base64.decodeBase64(b);
out.write(decoded);
}
}
ret = true;
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errorMessage = "error: " + sw;
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println("Cannot close outputStream after writing file to disk!" + sw.toString());
}
}
}
return ret;
}
/**
* Helper method for the creation of a file output stream.
*
* @param imageFolder
* : folder where images are to be saved.
* @param id
* : id of spcefic image file.
* @return FileOutputStream object prepared to store images.
* @throws FileNotFoundException
*/
protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException {
imageFile.getParentFile().mkdirs();
return new FileOutputStream(imageFile);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.