简体   繁体   English

捕获图像并将其从 Android 上传到 aws s3

[英]Capture an image and upload it to aws s3 from Android

I'm trying to capture an image from the camera and upload it to aws s3.我正在尝试从相机捕获图像并将其上传到 aws s3。 The image is captured and being displayed.图像被捕获并被显示。 But on touching upload image button, it fails to upload despite showing no error.但是在触摸上传图片按钮时,尽管没有显示错误,但它无法上传。 The progress dialog also doesn't close.进度对话框也不会关闭。

No error is being shown in the logs:日志中没有显示错误:

2020-08-14 18:56:22.425 32492-32520/com.example.photosaver I/mple.photosave: Waiting for a blocking GC ProfileSaver
2020-08-14 18:56:22.470 32492-32520/com.example.photosaver I/mple.photosave: WaitForGcToComplete blocked ProfileSaver on HeapTrim for 45.371ms
2020-08-14 18:56:22.485 32492-32520/com.example.photosaver I/mple.photosave: ProcessProfilingInfo new_methods=0 is saved saved_to_disk=0 resolve_classes_delay=8000
2020-08-14 18:56:26.759 32492-32492/com.example.photosaver W/Activity: Slow Operation: Activity com.example.photosaver/.MainActivity onActivityResult took 269ms
2020-08-14 18:56:26.783 32492-32502/com.example.photosaver W/System: A resource failed to call close. 
2020-08-14 18:56:28.921 32492-32492/com.example.photosaver V/S3Uploader: Upload file: /storage/emulated/0/Android/data/com.example.photosaver/files/Pictures/JPEG_20200814_185616_7789832864562633640.jpg
2020-08-14 18:56:28.921 32492-32492/com.example.photosaver V/MainActivity: initUpload successful
2020-08-14 18:56:28.921 32492-32492/com.example.photosaver V/S3Uploader: setUploadDone

Function to upload, called on touching the upload image button: Function 上传,点击上传图片按钮调用:

private void uploadImageTos3(Uri imageUri) {
        //final String path = getFilePathFromURI(imageUri);
        final String path = mCurrentPhotoPath;
        if (path != null) {
            showLoading();//uploading image..
            //call initUpload - sets file to upload
            s3uploaderObj.initUpload(path);

            Log.v("MainActivity", "initUpload successful" );
            s3uploaderObj.setOns3UploadDone(new S3Uploader.S3UploadInterface() {
                @Override
                public void onUploadSuccess(String response) {
                    Log.v("MainActivity", "OnUploadSuccess" );
                    if (response.equalsIgnoreCase("Success")) {

                        hideLoading();
                        urlFromS3 = S3Utils.generates3ShareUrl(getApplicationContext(), path);
                        if(!TextUtils.isEmpty(urlFromS3)) {
                            Toast.makeText(MainActivity.this, "Uploaded Successfully!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                @Override
                public void onUploadError(String response) {
                    hideLoading();
                    Log.e("MainActivity", "Error Uploading");

                }
            });
        }else{
            Toast.makeText(this, "Null Path", Toast.LENGTH_SHORT).show();
        }
    }

Functions in S3Uploader.java - S3Uploader.java 中的函数 -

private Context context;
    private TransferUtility transferUtility;
    public S3UploadInterface s3UploadInterface;

    public S3Uploader(Context context) {
        this.context = context;
        transferUtility = AmazonUtil.getTransferUtility(context);

    }

    public void initUpload(String filePath) {


        File file = new File(filePath);
        ObjectMetadata myObjectMetadata = new ObjectMetadata();
        myObjectMetadata.setContentType("image/png");
        String mediaUrl = file.getName();
        TransferObserver observer = transferUtility.upload(AWSKeys.BUCKET_NAME, mediaUrl,
                file, CannedAccessControlList.PublicRead);
        observer.setTransferListener(new UploadListener());

        Log.v(TAG, "Upload file: "+filePath);
    }

public void setOns3UploadDone(S3UploadInterface s3UploadInterface) {

        this.s3UploadInterface = s3UploadInterface;
        Log.v(TAG, "setUploadDone");
    }

    public interface S3UploadInterface {
        void onUploadSuccess(String response);

        void onUploadError(String response);

    }

Please have a look at Documentation请查看文档

You can upload image to s3 using below method.您可以使用以下方法将图像上传到 s3。

Amplify.Storage.uploadFile(
                "your filename",
                path,
                result -> {
                    hideLoading();
                    Log.i("MyAmplifyApp", "Image Successfully uploaded: " + result.getKey());      
                },
                storageFailure -> {
                    hideLoading();
                    Log.e("MyAmplifyApp", "Image Upload failed", storageFailure);
                }
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM