Just Simple Info

Pages

Android Upload File To Server With Data

Upload file to server with data and progress bar
Sometimes our android application need to upload some file to our live server like images, text files, etc.
Uploading file to server is one of my worst task. Searching, testing and searching, testing and again and again. I spent a lot of time just to upload the image to server. After a day, lucky I uploaded my first file to server. What I done is, I convert bitmap to byte[] then convert to Base64String then send to server as POST. But the size of original size increase which is not good. It consume extra bandwidth and time of uploading.


Now I found an easy way on how to upload image or file to the server with some data.
So, now we are going to write simple code on how to upload file to server with string data and with progress bar.

We have to use extra library which are :
  1. httpmime-4.1.3.jar
  2. httpcore-4.1.4.jar
  3. httpclient-4.1.3.jar

Below is the our project structure.


In this project we created three java file which are:
  1. CustomMultiPartEntity.java
  2. MainActivity.java
  3. UploadProgressListener.java
If you are looking for xml file, we are not going to use xml for layout or for any data. We will just use plain java code.

Now, here is the code in CustomMultiPartEntity.java


package com.example.uploadimagetoserver;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

public class CustomMultiPartEntity extends MultipartEntity
{
 
 private UploadProgressListener uploadProgressListener;
 
 public CustomMultiPartEntity()
 {
  super();
  
 }
 
 public CustomMultiPartEntity(final HttpMultipartMode mode)
 {
  super(mode);
  
 }
 
 public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset)
 {
  super(mode, boundary, charset);
  
 }
 
 @Override
 public void writeTo(final OutputStream outstream) throws IOException
 {
  super.writeTo(new CountingOutputStream(outstream, this.uploadProgressListener));
  
  
 }
 
 /**
  * 
  * @return
  */
 public UploadProgressListener getUploadProgressListener() {
  return uploadProgressListener;
 }
 /**
  * 
  * @param uploadProgressListener
  */
 public void setUploadProgressListener(
   UploadProgressListener uploadProgressListener) {
  this.uploadProgressListener = uploadProgressListener;
 }


 /**
  * 
  * Count the OutputStream
  *
  */
 public static class CountingOutputStream extends FilterOutputStream
 {
 
  private final UploadProgressListener uploadProgressListener;
  private long transferred;
 
  public CountingOutputStream(final OutputStream out, final UploadProgressListener uploadProgressListener)
  {
   super(out);
   this.uploadProgressListener = uploadProgressListener;
   this.transferred = 0;
  }
 
  @Override
  public void write(byte[] b, int off, int len) throws IOException
  {
   out.write(b, off, len);
   this.transferred += len;
   
   if(this.uploadProgressListener !=null){
    
    this.uploadProgressListener.transferred(this.transferred);
    
   }
   
  }
  @Override
  public void write(int b) throws IOException
  {
   out.write(b);
   this.transferred++;
   
   if(this.uploadProgressListener !=null){
    
    this.uploadProgressListener.transferred(this.transferred);
    
   }
   
  }
 }
}

Here is the code in UploadProgressListener.java


package com.example.uploadimagetoserver;
/**
 * Upload Listener
 *
 */
public interface UploadProgressListener {
 /**
  * This method updated how much data size uploaded to server
  * @param num
  */
 void transferred(long num);
}

Here is the code in MainActivity.java


package com.example.uploadimagetoserver;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

 final static int REQUEST_CODE = 1;
 final static String[]BUTTON_LABEL = {"Select Image","Upload Image"};
 
 // CHANGE THIS TO YOUR URL
 final static String UPLOAD_SERVER_URI = "http://192.168.25.117/android/UploadToServer.php";
 
 ProgressDialog progressDialog;
 ScrollView scrollView;
 LinearLayout linearLayout;
 ImageView imageView;
 TextView  imageLocationTextView;
 Button selectImgBtn;
 Button uploadBtn;
 
 String imagePath;
 String imageName;
 
 long imageSize = 0; // kb
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setupLayout();
    }
    /**
     * 
     */
    private void setupLayout(){
    
     scrollView = new ScrollView(this);
     linearLayout = new LinearLayout(this);
     imageView = new ImageView(this);
     imageLocationTextView = new TextView(this);
     selectImgBtn = new Button(this);
     uploadBtn = new Button(this);
     
     
     selectImgBtn.setText(BUTTON_LABEL[0]);
     uploadBtn.setText(BUTTON_LABEL[1]);
     selectImgBtn.setOnClickListener(this);
     uploadBtn.setOnClickListener(this);
     
     linearLayout.setOrientation(LinearLayout.VERTICAL);
     linearLayout.addView(imageView);
     linearLayout.addView(imageLocationTextView);
     linearLayout.addView(selectImgBtn);
     linearLayout.addView(uploadBtn);
     
     scrollView.addView(linearLayout);
     
     this.setContentView(scrollView);
    }
    /**
     * 
     * @return
     */
    private ProgressDialog createDialog(){
     ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait.. Uploading File");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);
        
        return progressDialog;
    }
 
 @Override
 public void onClick(View view) {
  
  String viewLabel = ((Button)view).getText().toString();
  
  if(viewLabel.equalsIgnoreCase(BUTTON_LABEL[0])){
   // SELECT IMAGE
   
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Image"), REQUEST_CODE);
            
  }else{
   
   // UPLOAD IMAGE
   
   if(this.imagePath == null){
    // IF NO IMAGE SELECTED DO NOTHING
    Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
    return;
   }
   
   this.progressDialog = this.createDialog();
   this.progressDialog.show();
   
   // EXECUTED ASYNCTASK TO UPLOAD IMAGE
   new ImageUploader().execute();
    
  }
  
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  
  if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
   
   Uri selectedImageUri = data.getData();
   
   // GET IMAGE PATH
   imagePath = getPath(selectedImageUri);
   
   
   // IMAGE NAME
   imageName = imagePath.substring(imagePath.lastIndexOf("/"));
   
   imageSize = this.getFileSize(imagePath);
            
   // DECODE TO BITMAP
   Bitmap bitmap=BitmapFactory.decodeFile(imagePath);
   
   // DISPLAY IMAGE
   imageView.setImageBitmap(bitmap);
   imageLocationTextView.setText("File path :" +imagePath);
            
  }
 }
 
 /**
  * Get the image path
  * @param uri
  * @return
  */
 private String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
 /**
  * Get the file size in kilobytes
  * @return
  */
 private long getFileSize(String imagePath){
  
   long length = 0;

  try {

   File file = new File(imagePath);
   length = file.length();
   length = length / 1024;

  } catch (Exception e) {

   e.printStackTrace();
  }

  return length;
 }
 
 /**
  * This class is responsible for uploading data
  * @author lauro
  *
  */
  private class ImageUploader extends AsyncTask<Void, Integer, Boolean> implements UploadProgressListener {

   @Override
   protected Boolean doInBackground(Void... params) {
       
          try{
           
             InputStream inputStream = new FileInputStream(new File(imagePath));
              
              //*** CONVERT INPUTSTREAM TO BYTE ARRAY
               
              byte[] data = this.convertToByteArray(inputStream);
                   
              HttpClient httpClient = new DefaultHttpClient();
              httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,System.getProperty("http.agent"));
             
              HttpPost httpPost = new HttpPost(UPLOAD_SERVER_URI);
             
              // STRING DATA
              StringBody dataString = new StringBody("This is the sample image");
             
              // FILE DATA OR IMAGE DATA
              InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data),imageName);
              
             // MultipartEntity multipartEntity = new MultipartEntity();
              CustomMultiPartEntity  multipartEntity = new CustomMultiPartEntity();
              
              // SET UPLOAD LISTENER
              multipartEntity.setUploadProgressListener(this);
             
              //*** ADD THE FILE
              multipartEntity.addPart("file", inputStreamBody);
                  
              //*** ADD STRING DATA
              multipartEntity.addPart("description",dataString);   
              
              httpPost.setEntity(multipartEntity);
              
              // EXECUTE HTTPPOST
              HttpResponse httpResponse = httpClient.execute(httpPost);
              
              // THE RESPONSE FROM SERVER
              String stringResponse =  EntityUtils.toString(httpResponse.getEntity());
              
              // DISPLAY RESPONSE OF THE SERVER
              Log.d("data from server",stringResponse);
              
             
             
          } catch (FileNotFoundException e1) {
              e1.printStackTrace();
              
              return false;
              
          } catch (ClientProtocolException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     
     return false;
     
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     
     return false;
    }
   
     return true;
   }
   
   /**
    * 
    */
   @Override
   public void transferred(long num) {
    
    // COMPUTE DATA UPLOADED BY PERCENT
    
    long dataUploaded = ((num / 1024) * 100 ) / imageSize;
     
    // PUBLISH PROGRESS
    
    this.publishProgress((int)dataUploaded);
    
   }
   
   /**
    * Convert the InputStream to byte[]
    * @param inputStream
    * @return
    * @throws IOException
    */
   private byte[] convertToByteArray(InputStream inputStream) throws IOException{
    
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
                
         int next = inputStream.read();
         while (next > -1) {
               bos.write(next);
               next = inputStream.read();
          }
                
         bos.flush();
                
         return bos.toByteArray();
   }
   
   
   
   @Override
   protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    
    // UPDATE THE PROGRESS DIALOG
    
    progressDialog.setProgress(values[0]);
    
    
    
   }

   @Override
   protected void onPostExecute(Boolean uploaded) {
    // TODO Auto-generated method stub
    super.onPostExecute(uploaded);
    
    
    if( uploaded){
     
     // UPLOADING DATA SUCCESS
     
     progressDialog.dismiss();
     Toast.makeText(MainActivity.this, "File Uploaded", Toast.LENGTH_SHORT).show();
     
    
    }else{
     
     // UPLOADING DATA FAILED
     
     progressDialog.setMessage("Uploading Failed");
     progressDialog.setCancelable(true);
     
     
    }
    
    
   }

   

  }
 
 
}

Here is the Php code to our server.


<?php
 
    // DISPLAY FILE INFORMATION JUST TO CHECK IF FILE OR IMAGE EXIST
    echo '<pre>';
    print_r($_FILES);
    echo '</pre>';
    
    // DISPLAY POST DATA JUST TO CHECK IF THE STRING DATA EXIST
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    
    $file_path = "images/";
    $file_path = $file_path . basename( $_FILES['file']['name']);
    
    if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {
       
        echo "file saved success";
    
       
    } else{
        
       echo "failed to save file";
    }
?>

After uploading check your logcat for the server response message. The message is just for debugging purpose only.
Here is the message look like.



Download Source Code Here.

See my other blog.

1. Speed up Large Data Insertion in Sqlite Database in Android Application
2. Android Scrolling Table with Fixed Header Column, Fixed Row , Header Spans and Pagination.
3. Android Calling Javascript function from Java function and Javascript function to Java function in Webview.
4. Android Use Sqlite Database Example
5. Android LinearLayout Example
Happy coding. :)

13 comments:

  1. can you send me this code to my mail id.becoze im using the same code in my project

    ReplyDelete
  2. Hey Dear, i am new in android. i have made an application that communicate between user by text messages using sockets and xampp server. now i want to attach some files like image and send it to other user... please help me.... thanks for any help...

    ReplyDelete
  3. please send me server side php code , I am getting error in php file.

    ReplyDelete
    Replies
    1. See the php code above. That is exactly the code I used.

      Delete
  4. Hello, thanks for the tutorial. My application got run and it show that file uploaded. But the apps did not create the file for uploaded image. Can you help me with that ? Thank you :)

    ReplyDelete
  5. Can you tell how to create server side to receive the image in JAVA.

    ReplyDelete
  6. What is the code for upload any file (like doc's, image, video, zip etc.)?

    ReplyDelete
  7. D/data from server:

    400 Bad Request
    Your browser sent a request that this server could not understand.

    ReplyDelete