title: Convert base64 string to Image in Java
body: <p>I have an image being sent to me through a JSON string.  I want to convert that string into an image in my android app and then display that image.</p>
  
  <p>The JSON string looks like this:</p>
  
  <pre><code>"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
  </code></pre>
  
  <p>Note: I truncated the string with a ...</p>
  
  <p>I've got a function that (I think) converts the string into an image.  Am I doing this right?</p>
  
  <pre><code>public Bitmap ConvertToImage(String image){
      try{
          InputStream stream = new ByteArrayInputStream(image.getBytes());
          Bitmap bitmap = BitmapFactory.decodeStream(stream);                 
          return bitmap;  
      }
      catch (Exception e) {
          return null;            
      }
  }
  </code></pre>
  
  <p>Then I try to display it on my android activity like this</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>However, when I do this, nothing shows up.  I don't get any errors in the logcat.  What am I doing wrong?</p>
  
  <p>Thanks</p>