我的 Android 应用程序可以将相机直接拍摄的照片或从图库中选择的照片上传到 S3 AWS。现在我希望该应用程序也可以从图库上传视频。但是上传的文件大小总是0Kb。
这是视频选择过程的代码:
Intent videoPickerIntent = new Intent(Intent.ACTION_PICK);
videoPickerIntent.setType("video/*");
getActivity().startActivityForResult(videoPickerIntent, SELECT_VIDEO);
然后,我得到了视频路径(我已经测试了我在同一个应用程序的 VideoView 中播放视频的路径)。 (示例:“/storage/emulated/0/DCIM/Camera/VID_20140903_163147.mp4”)
最后,我在 AsyncTask 中使用 PutObjectRequest,类似于我上传照片的方式。
PutObjectRequest por = new PutObjectRequest(bucket, nameInAwsWithPath, videoPath);
por.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(por);
然后,在 S3 中,我可以看到一个按预期命名但为空的文件。 0 KB 大小。
出了什么问题?
最佳答案
这是代码:
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();
}
}
}
关于android - 从 Android 应用程序将视频上传到 S3 AWS 会生成一个 0Kb 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25669766/