title: Send a Base64 encoded image through HTTP Post Request from Android
body: <p>I am completely lost, I am trying to send a photo via an android app to a php webpage.</p>
  
  <p>Theoritically everything should be right but the destination data is corrupted or something I don't know... </p>
  
  <p>I can get the post data, I tried with a simple string it worked fine but with a heavy file the data seems corrupted. </p>
  
  <pre><code>public class EncodingAndSending extends Thread{
      ShareOnMyWebSiteActivity mycontext;
      ContentResolver cr;
      Uri uri;
      public Handler mainHandler,sHandler;
  
      public EncodingAndSending(ShareOnMyWebSiteActivity myctxt,Handler mainone,Uri URI,ContentResolver crr){
           mycontext=myctxt;
           cr=crr;
           uri=URI;
           mainHandler=mainone;
           this.start();
      }
  
          return buffer.toString().toUpperCase();    
        }
  
      public void run(){
  
          InputStream is=null;
          byte[] data=null;
          try {
              is = cr.openInputStream(uri);
               // Get binary bytes for encode
              data = getFileBytes(is);
          } catch (Exception e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          String data_string=Base64.encodeToString(data,Base64.URL_SAFE);
  
  
          if(data_string!=""){
              SendRequest(data_string);
          }
          else{
  
          }
  
      }
  
  public byte[] getFileBytes(InputStream ios) throws IOException {
              ByteArrayOutputStream ous = null;
              //InputStream ios = null;
              try {
                  byte[] buffer = new byte[4096];
                  ous = new ByteArrayOutputStream();
                  //ios = new FileInputStream(file);
                  int read = 0;
                  while ((read = ios.read(buffer)) != -1)
                      ous.write(buffer, 0, read);
              } finally {
                  try {
                      if (ous != null)
                          ous.close();
                  } catch (IOException e) {
                      // swallow, since not that important
                  }
                  try {
                      if (ios != null)
                          ios.close();
                  } catch (IOException e) {
                      // swallow, since not that important
                  }
              }
              return ous.toByteArray();
          }
  
  
      private void SendRequest(String data_string){
  
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost("xxxxx.php");
  
          try {
  
              List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
              nameValuePairs.add(new BasicNameValuePair("image", data_string));
  
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
              // Execute HTTP Post Request
              HttpResponse response = httpclient.execute(httppost);
              ResponseHandler&lt;String&gt; responseHandler=new BasicResponseHandler();
              String responseBody = httpclient.execute(httppost, responseHandler);
  
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
          }       
      }
  
  }
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>This works. I can encode and decode and preview the image. I didn't use the getBytes() function I don't know if the problem comes from there.</p>
  
  <p>I'll let you know.</p>
  
  <pre><code>    super.onCreate(savedInstanceState);
      setContentView(R.layout.test_image);
      ImageView image = (ImageView) findViewById(R.id.imageView1);
      FileInputStream in;
      BufferedInputStream buf;
  
      Intent intent = getIntent();
      Bundle extras = intent.getExtras();
  
      Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
      ContentResolver cr = getContentResolver();
      Bitmap bMap=null;
      try {
          InputStream is = cr.openInputStream(uri);
  
          bMap = BitmapFactory.decodeStream(is);
  
          if (is != null) {
          is.close();
          }
      } catch (Exception e) {
          Log.e("Error reading file", e.toString());
      }
  
      ByteArrayOutputStream baos = new ByteArrayOutputStream();  
      bMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
      byte[] b = baos.toByteArray();
      String data_string=Base64.encodeToString(b,Base64.DEFAULT);
  
      b=null;bMap=null;
      b=Base64.decode(data_string, Base64.DEFAULT);
      bMap=BitmapFactory.decodeByteArray(b, 0, b.length);
      image.setImageBitmap(bMap);
  </code></pre>