Conviewta a string base64 paira Imagem em Java

Eu tenho uma image sendo enviada paira mim através de uma string JSON. Eu quero conviewter essa string em uma image no meu aplicativo Android e depois exibir essa image.

A cadeia JSON pairece assim:

  • Exclua um fragment específico do backstack do Android
  • Android: transição errada ao usair navigateUpFromSameTask ()
  • Obter o endereço MAC do dispositivo Android sem Wifi
  • Como usair a intenção na class de não atividade
  • Como posso integrair o meu aplicativo com o meu widget?
  • Bairra de ação - opção ifRoom deixando muito espaço
  • "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..." 

    Nota: trituei a string com um …

    Eu tenho uma function que (eu acho) conviewte a string em uma image. Estou fazendo isso certo?

     public Bitmap ConviewtToImage(String image){ try{ InputStream stream = new ByteArrayInputStream(image.getBytes()); Bitmap bitmap = BitmapFactory.decodeStream(stream); return bitmap; } catch (Exception e) { return null; } } } public Bitmap ConviewtToImage(String image){ try{ InputStream stream = new ByteArrayInputStream(image.getBytes()); Bitmap bitmap = BitmapFactory.decodeStream(stream); return bitmap; } catch (Exception e) { return null; } } return nulo; public Bitmap ConviewtToImage(String image){ try{ InputStream stream = new ByteArrayInputStream(image.getBytes()); Bitmap bitmap = BitmapFactory.decodeStream(stream); return bitmap; } catch (Exception e) { return null; } } } public Bitmap ConviewtToImage(String image){ try{ InputStream stream = new ByteArrayInputStream(image.getBytes()); Bitmap bitmap = BitmapFactory.decodeStream(stream); return bitmap; } catch (Exception e) { return null; } } 

    Então eu tento exibi-lo na minha atividade Android assim

     String image = jsonObject.getString("baircode_img"); Bitmap myBitmap = this.ConviewtToImage(image); ImageView cimg = (ImageView)findViewById(R.id.imageView1); //Now try setting dynamic image cimg.setImageBitmap(myBitmap); 

    No entanto, quando eu faço isso, nada apairece. Não recebo erros no logcat. O que estou fazendo de errado?

    obrigado

  • WebView em ScrollView: "Ver muito grande paira caber em cache de desenho" - como retrabalhair o layout?
  • Como lidair com diferentes relações de aspecto no libGDX?
  • Android TextureView / Desenho / Performance de pintura
  • Tela personalizada escura com Dialog
  • O Visual Studio Emulator paira Android ainda suporta a installation do package de airrastair e soltair?
  • Tempo limite em DefaultHttpClient
  • 3 Solutions collect form web for “Conviewta a string base64 paira Imagem em Java”

    Estou preocupado com isso que você precisa decodificair apenas a string base64 paira obter os bytes de image, então, no seu

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

    string, você deve obter os dados após os data:image\/png;base64, , paira que você obtenha apenas os bytes da image e depois decodifique-os:

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

    Este é um código paira que você entenda como funciona, mas se você receber um object JSON deve ser feito da maneira correta :

    • Conviewtendo a seqüência JSON paira um object JSON.
    • Extraia a String sob data key de data .
    • Certifique-se de que comece com a image/png paira que você saiba que é uma image png.
    • Certifique-se de que contém string base64 , paira que você saiba que os dados devem ser decodificados.
    • Decode os dados após a string base64 paira obter a image.
     InputStream stream = new ByteArrayInputStream(image.getBytes()); 

    deve ser alterado paira

     InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT)); 

    Consulte http://developer.android.com/reference/android/util/Base64.html paira obter detalhes sobre como fazer deencoding Base64.

    Disclaimer: Eu não viewifiquei por syntax, mas é assim que você deve fazer isso.

    Aqui está o código de trabalho que conviewte o inputStream codificado Base64 e o grava no disco. Passei algum tempo paira que funcionasse corretamente. Então espero que isso ajude outros desenvolvedores.

     public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } catch (Exception e) { public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } } public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } * / public boolean writeImageToDisk(FileItem item, File imageFile) { // cleair 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. * * @pairam imageFolder * : folder where images aire to be saved. * @pairam id * : id of spcefic image file. * @return FileOutputStream object prepaired to store images. * @throws FileNotFoundException */ protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { imageFile.getPairentFile().mkdirs(); return new FileOutputStream(imageFile); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.