0

I want to compress a video/audio file before sending to the server because it takes too much time to upload. I am using a multipart entity with a string body to upload the file on server.

My code is below. Please give me any possible solutions to this issue. This code is working, but takes too much time. Could anyone help me improve the performance?

try {
    if (selectedPath == null || selectedPath == "") {

        getActivity().runOnUiThread(new Runnable() {
               public void run() {

                    Alert_Dialogs.custom_alert_dialog(getActivity(),"Please select Video.");

               }
             });    
    }
    else {

    File file = new File(selectedPath);

    StringBody title = new StringBody(et_title.getText().toString().trim());
    StringBody catId = new StringBody(cat_id);
    StringBody user_data = new StringBody(user_id);

        FileBody filebodyVideo = new FileBody(file);
    CustomMultiPartEntity customMultiPartEntity = new CustomMultiPartEntity(new ProgressListener() {

        @Override
        public void transferred(long num) {

            publishProgress((int) ((num / (float) totalSize) * 100));                               }
    });


    customMultiPartEntity.addPart("title",title );
    customMultiPartEntity.addPart("catId",catId );
    customMultiPartEntity.addPart("user_id",user_data );
    customMultiPartEntity.addPart("qqfile", filebodyVideo);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Application.UploadVideosEndpoint());
    //  static_url+"WS/videoUpload?api="+static_api_key

    totalSize = customMultiPartEntity.getContentLength();
    httppost.setEntity(customMultiPartEntity);


    HttpResponse response = httpclient.execute( httppost );
    HttpEntity resEntity = response.getEntity( );


    str_response = EntityUtils.toString(resEntity);



    if (resEntity != null) {
      resEntity.consumeContent( );
    }

    httpclient.getConnectionManager( ).shutdown( );
}


} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Will
  • 24,082
  • 14
  • 97
  • 108

1 Answers1

0

You can make .zip file of your data to be sent. By making zip data will get compressed (so the size reduced).

Here is the working example with code which will help you.

zip/unzip file in android

Jyo the Whiff
  • 829
  • 9
  • 23
  • hello thanks for reply....but i don't want make a zip file..is it any other solution to compress file without zipping ?? – Sahil Bansal May 18 '15 at 05:23