将base64string转换为Java中的Image

我有一个图像通过JSONstring发送给我。 我想在我的android应用程序中将该string转换为图像,然后显示该图像。

JSONstring如下所示:

"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..." 

注意:我用…截断了string

我有一个函数(我认为)将string转换为图像。 我做对了吗?

 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中出现任何错误。 我究竟做错了什么?

谢谢

我担心你只需要解码base64string来获取图像字节,所以在你的

 "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..." 

string,你必须得到数据后的data:image\/png;base64, ,,所以你只得到图像字节,然后解码他们:

 String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1); InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT)); 

这是一个代码,所以你了解它是如何工作的,但是如果你收到一个JSON对象,应该按照正确的方式完成:

  • 将JSONstring转换为JSON对象。
  • data键下提取string。
  • 确保从image/png开始,所以你知道是一个PNG图像。
  • 确保包含base64string,所以你知道数据必须被解码。
  • base64string之后解码数据以获取图像。
 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); }