【问题标题】:Uploading a video to S3 AWS from Android app results in a 0Kb file从 Android 应用程序将视频上​​传到 S3 AWS 会生成 0Kb 文件
【发布时间】:2014-10-29 10:56:54
【问题描述】:

我的 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 大小。

怎么了?

【问题讨论】:

  • 如果您在运行此代码时打开了 logcat,是否有任何异常或错误消息?有可能在单独的线程上引发了异常,使其不明显发生。
  • logcat 中完全没有错误或警告。 putobject 在 try/catch 中,一切似乎都很顺利......但我在 AWS 上得到了空文件。
  • 现在我已经声明了:PutObjectRequest por;在 doInBackground 方法的开头并在最后实例化它: por = new PutObjectRequest(bucket, nameInAwsWithPath, videoPath); por.setCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(por);它有效
  • 很高兴听到您成功了!
  • 你能把整个代码贴出来吗?我遇到了同样的麻烦/不知道如何从 Android 上传我的视频我的 S3 :(

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


【解决方案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();
            }
        }
    }

【讨论】:

  • 是@user6042879 要求的代码(显示我当时如何纠正问题的答案)
  • 因此您可能应该接受它以明确表示问题已解决。
  • 谢谢!您还没有检查此代码是否有效?现在这一定是很老的代码了哈哈:D
  • 已经一年半了!该按钮刚刚从应用程序中消失了:D
猜你喜欢
  • 2014-07-28
  • 2018-09-30
  • 2015-04-21
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多