从Android应用上传视频到S3 AWS会产生0Kb文件

时间:2014-09-04 15:47:23

标签: android video file-upload amazon-web-services amazon-s3

我的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尺寸。

出了什么问题?

1 个答案:

答案 0 :(得分:1)

这是代码:

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