I currently have an app that allows the user to record a video with the device camera, after they are done recording, the video uploads to a server. I am currently using the following code to upload the video and it works good, the only issue I am having is that it is taking way too long. A 30 second video can take about 20 minutes to upload. Is there any way to use a different and faster method to upload these videos? I have looked everywhere and I am only finding posts with the same code as I am using. This is my code:
public class UploadActivity extends Activity {
private String filePath = null;
private String name, email, videoTitle;
long totalSize = 0;
private DonutProgress donutProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
donutProgress = (DonutProgress) findViewById(R.id.donut_progress);
Intent intent = getIntent();
filePath = intent.getStringExtra("filePath");
name = intent.getStringExtra("name");
email = intent.getStringExtra("email");
new UploadFileToServer().execute();
}
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
donutProgress.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
donutProgress.setProgress(progress[0]);
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(AppConfig.URL_UPLOAD);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
entity.addPart("image", new FileBody(sourceFile));
totalSize = entity.getContentLength();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred. Http Status Code: " + statusCode;
}
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
videoTitle = result;
super.onPostExecute(result);
finish();
}
}
}