Reputation: 2783
I am have a functionality to upload a video in android. For that I have taken the entire code in a service and inside that used an AsyncTask to call upload video function in doInBackground(). Below is the code to upload video:
private String uploadFile(String filePath, String proid, String procat) {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Const_Url.FILE_UPLOAD_URL);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
entity.addPart("project_id", new StringBody(proid));
entity.addPart("user_id", new StringBody(userid));
entity.addPart("video_type", new StringBody("in_store"));
Log.d("background file path ", filePath);
Log.d("background project id", proid);
Log.d("background project cat", procat);
Log.d("background user id", userid);
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
Hence AndroidMultiPartEntity uploads entire video.
But the issue is that if in between I am trying to make a HTTP call to server, then it just waits till the video upload to finish().
Ideally it shouldn't wait till taht time, but respond me asap. I think Video upload service grabs the entire bandwidth and hence other HTTP calls not being made.
Please suggest some solutions. TIA.
P.S. I have tried
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
To lower the priority of that AsyncTask and increasing the priority of other http calls(Asynctask). But in vain.
Upvotes: 0
Views: 90
Reputation: 2783
To everyone who face this problem, I have got a solution. If you want to upload a very heavy file (~500mb) in background with the help of service, please use this library: android upload service
I have found this extremely easy to integrate and it also gives many additional features like showing progressbar in notification bar
Upvotes: 1