title: How to decode the image string to bitmap image from php server?
body: <p>In my application i have to select an image from gallery and set it as an profile picture. For that i have encoded the image as string and send it to the server. After getting the response from server i want to decode the string image to bitmap image.While receiving response from the server it shows the following error  </p>
  
  <pre><code>java.lang.IllegalArgumentException: bad base-64
  at android.util.Base64.decode(Base64.java:161)
  at android.util.Base64.decode(Base64.java:136)
  at android.util.Base64.decode(Base64.java:118) 
  </code></pre>
  
  <p>Below code i use the base64 method to convert the bitmap image to string, </p>
  
  <pre><code>@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK
                  &amp;&amp; null != data) {
              Uri selectedImage = data.getData();
              String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
              Cursor cursor = getContentResolver().query(selectedImage,
                      filePathColumn, null, null, null);
              cursor.moveToFirst();
  
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String picturePath = cursor.getString(columnIndex);
              cursor.close();
              profileimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
              profileimage.buildDrawingCache();
              Bitmap bm = profileimage.getDrawingCache();
  
              ByteArrayOutputStream bao = new ByteArrayOutputStream();
  
              // Resize the image
              double width = bm.getWidth();
              double height = bm.getHeight();
              double ratio = 100 / width;
              int newheight = (int) (ratio * height);
  
              System.out.println("———-width" + width);
              System.out.println("———-height" + height);
              System.out.println("———-height" + newheight);
  
              bm = Bitmap.createScaledBitmap(bm, 100, newheight, true);
  
              // Here you can define .PNG as well
              bm.compress(Bitmap.CompressFormat.JPEG, 95, bao);
              byte[] ba = bao.toByteArray();
              ba1 = Base64.encodeToString(ba, Base64.NO_WRAP);
  
              System.out.println("uploading image now ——–" + ba1);
  
          }
  
      }
  </code></pre>
  
  <p>Code for server call</p>
  
  <pre><code>public String makeServiceCall1(String url1, String a, final Context context) {
          this.url = url1;
          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
          nameValuePairs.add(new BasicNameValuePair("profileimage", a));
          try {
  
              if (!isConnected(context)) {
  
                  return url;
              }
              if (isConnected(context) == false) {
                  Toast.makeText(context, "No Connection Available", 500).show();
              }
  
              URI uri = new URI(url.replace(" ", "%20"));
              Log.d("uri", url + "");
  
              HttpPost httppost = new HttpPost(uri);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpParams httpParameters = httppost.getParams();
  
              int timeoutConnection = 100000;
              HttpConnectionParams.setConnectionTimeout(httpParameters,
                      timeoutConnection);
  
              int timeoutSocket = 100000;
              HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
  
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
  
              List&lt;Cookie&gt; cookies = httpclient.getCookieStore().getCookies();
              if (cookies.isEmpty()) {
                  System.out.println("None");
              } else {
                  for (int i = 0; i &lt; cookies.size(); i++) {
                      System.out.println("- " + cookies.get(i).toString());
                  }
              }
              int responseCode = response.getStatusLine().getStatusCode();
  
              if (response != null) {
                  msg = EntityUtils.toString(entity);
  
              }
              Log.d("serverResponse", msg);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return msg;
          // TODO Auto-generated method stub
  
      }
  </code></pre>
  
  <p>Code for convert the image sting to bitmap image,</p>
  
  <pre><code>byte[] decodedString = Base64.decode(image, Base64.NO_WRAP);
      Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
              decodedString.length);
  profilepic.setImageBitmap(decodedByte);
  </code></pre>
  
  <p>Please any one help me to solve the issue.</p>