简体   繁体   中英

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. 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:

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 -

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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