[英]Convert base64 string to Image in Java
I have an image being sent to me through a JSON string. 我有一个图像通过JSON字符串发送给我。 I want to convert that string into an image in my android app and then display that image. 我想将该字符串转换为我的Android应用程序中的图像,然后显示该图像。
The JSON string looks like this: JSON字符串如下所示:
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
Note: I truncated the string with a ... 注意:我用...截断了字符串
I've got a function that (I think) converts the string into an image. 我有一个函数(我认为)将字符串转换为图像。 Am I doing this right? 我这样做了吗?
public Bitmap ConvertToImage(String image){
try{
InputStream stream = new ByteArrayInputStream(image.getBytes());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
return bitmap;
}
catch (Exception e) {
return null;
}
}
Then I try to display it on my android activity like this 然后我尝试在我的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);
However, when I do this, nothing shows up. 但是,当我这样做时,什么都没有出现。 I don't get any errors in the logcat. 我没有在logcat中得到任何错误。 What am I doing wrong? 我究竟做错了什么?
Thanks 谢谢
I'm worried about that you need to decode only the base64 string to get the image bytes, so in your 我担心你只需解码base64字符串来获取图像字节,所以在你的
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
string, you must get the data after data:image\\/png;base64,
, so you get only the image bytes and then decode them: 字符串,你必须得到数据之后的data:image\\/png;base64,
,所以你只得到图像字节然后解码它们:
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
This is a code so you understand how it works, but if you receive a JSON object it should be done the correct way: 这是一个代码,所以你了解它是如何工作的,但如果你收到一个JSON对象,它应该以正确的方式完成:
data
key. 在data
键下提取String。 image/png
so you know is a png image. 确保以image/png
开头,因此您知道是png图像。 base64
string, so you know that data must be decoded. 确保包含base64
字符串,因此您知道必须解码数据。 base64
string to get the image. 在base64
字符串之后解码数据以获取图像。 InputStream stream = new ByteArrayInputStream(image.getBytes());
should be changed to 应改为
InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT));
Refer http://developer.android.com/reference/android/util/Base64.html for details on how to do Base64 decoding. 有关如何进行Base64解码的详细信息,请参阅http://developer.android.com/reference/android/util/Base64.html 。
Disclaimer: I have not checked for syntax, but this is how you should do it. 免责声明:我没有检查语法,但这是你应该怎么做。
Here is the working code that converts the Base64 encoded inputStream and writes it to the disk. 以下是转换Base64编码的inputStream并将其写入磁盘的工作代码。 I spent a few time to make it work correctly. 我花了一些时间让它正常工作。 So I hope this helps other developers. 所以我希望这有助于其他开发人员。
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.