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();
}