title: How to convert a Base64 string to Bitmap on Android?
body: <p>I'm having this problem for converting several Base64 string to  Bitmap images, in my android application.
  Now I've looked for solutions for quite a while, but nothing seems to solve my problem.
  My application gets JSON data from a server, containing several images in base64. the json code received from the server can be found here : <a href="http://pastebin.com/tgiMgqRg" rel="nofollow">http://pastebin.com/tgiMgqRg</a>
  I tried to decode the first image on this website : <a href="http://www.opinionatedgeek.com/dotnet/tools/base64decode/" rel="nofollow">http://www.opinionatedgeek.com/dotnet/tools/base64decode/</a>, and it seems to be correct.</p>
  
  <p>the code to receive the JSON date is :</p>
  
  <pre><code>    public JSONObject makeHttpRequest(String url, String method,
          List&lt;NameValuePair&gt; params) {
  
      // Making HTTP request
      try {
          HttpClient httpClient = sslClient(new DefaultHttpClient());
  
          // check for request method
          if(method == "POST"){
              // request method is POST
              //DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
              httpPost.setEntity(new UrlEncodedFormEntity(params));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          }else if(method == "GET"){
              // request method is GET
              //DefaultHttpClient httpClient = new DefaultHttpClient();
              String paramString = URLEncodedUtils.format(params, "utf-8");
              url += "?" + paramString;
              HttpGet httpGet = new HttpGet(url);
              //DEBUG
              Log.d("JSONParser", "sent request : " + url);
  
              HttpResponse httpResponse = httpClient.execute(httpGet);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
          }           
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "UTF-8"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
      } catch (Exception e) {
          Log.e("Buffer Error", "Error converting result " + e.toString());
      }
  
      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);
      } catch (JSONException e) {
          Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
  
      // return JSON String
      return jObj;
  
  }
  </code></pre>
  
  <p>I then try to decode these images using this code :</p>
  
  <pre><code>    try {
         images = json.getJSONArray(MapActivity.TAG_LIST);
  
              // looping through all Images
              for (int i = 0; i &lt; images.length(); i++) {
                  JSONObject house = images.getJSONObject(i);
                  String photo = house.getString(TAG_PHOTO);
                  //Log.d("PHOTO", photo);
                  Log.d("PHOTO NUMBER", i + "");
  
                  try {
                      byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);
                      Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                      HashMap&lt;String, Bitmap&gt; map = new HashMap&lt;String, Bitmap&gt;();
                      map.put(TAG_PHOTO, decodedByte);
  
                      imagesList.add(map);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>But i get these errors :</p>
  
  <pre><code>    D/PHOTO NUMBER(26854): 0
  D/skia(26854): --- SkImageDecoder::Factory returned null
  D/PHOTO NUMBER(26854): 1
  D/skia(26854): --- SkImageDecoder::Factory returned null
  D/PHOTO NUMBER(26854): 2
  D/skia(26854): --- SkImageDecoder::Factory returned null
  D/PHOTO NUMBER(26854): 3
  W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:136)
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:118)
  02W/System.err(26854):  at ...$LoadImages.doInBackground(DisplayHouseInfo.java:225)
  W/System.err(26854):    at ...$LoadImages.doInBackground(DisplayHouseInfo.java:1)
  W/System.err(26854):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
  W/System.err(26854):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  W/System.err(26854):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  W/System.err(26854):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  W/System.err(26854):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  W/System.err(26854):    at java.lang.Thread.run(Thread.java:841)
  D/PHOTO NUMBER(26854): 4
  W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                              ...
  D/PHOTO NUMBER(26854): 5
  W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                              ...
  D/PHOTO NUMBER(26854): 6
  W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
  W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                              ...
  02-27 14:57:11.316: E/BitmapFactory(26854): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
  I/System.out(26854): resolveUri failed on bad bitmap uri: 
  E/BitmapFactory(26854): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
  I/System.out(26854): resolveUri failed on bad bitmap uri: 
  </code></pre>
  
  <p>My problems are : 
  - I can't seem to make BitmapFactory.decodeByteArray work
  - I get another exception for the lasts images</p>
  
  <p>Do you have any ideas/suggestions on how to solve this ?</p>