My Android app can upload photos to S3 AWS taken by the camera directly or chosen from the galery. Now I want the app to upload videos from the gallery as well. But the uploaded file size is always 0Kb.
This is the code for the video picking process:
Intent videoPickerIntent = new Intent(Intent.ACTION_PICK);
videoPickerIntent.setType("video/*");
getActivity().startActivityForResult(videoPickerIntent, SELECT_VIDEO);
Then, I get the video path (I've tested the path I get playing the video inside a VideoView in the same app). (example: "/storage/emulated/0/DCIM/Camera/VID_20140903_163147.mp4")
And, finally, I use a PutObjectRequest in an AsyncTask, similarly to the way I upload photos.
PutObjectRequest por = new PutObjectRequest(bucket, nameInAwsWithPath, videoPath);
por.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(por);
And then, in S3 I can see a file, named as expected, but empty. 0 Kb size.
What's going wrong?
This was the code:
private class S3PutVideoTask extends AsyncTask<String, Integer, Boolean> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(getActivity());
dialog.setMessage(getString(R.string.uploadingvideo));
dialog.setCancelable(false);
dialog.show();
}
protected Boolean doInBackground(String... params) {
boolean result = true;
// Put the video data into S3.
try {
PutObjectRequest por;
String filename = "img_" + xxxxx[pos].id + "_"
+ System.currentTimeMillis() + ".avi";
File fileToUpload = new File(mCurrentAbsoluteVideoPath);
SharedPreferences prefs = getActivity().getSharedPreferences("GlobalPrefs", Context.MODE_PRIVATE);
String bucket = prefs.getString("bucket", PICTURE_BUCKET);
por = new PutObjectRequest(bucket, filename, fileToUpload);
por.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(por);
} catch (Exception exception) {
result = false;
}
return result;
}
@Override
protected void onPostExecute(Boolean result) {
dialog.dismiss();
if (!result) {
//show error
} else {
fileUploaded = true;
dismissUpload();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With