Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将图片上传到AWS s3 bucket?_Android_Amazon S3_Aws Sdk - Fatal编程技术网

Android 如何将图片上传到AWS s3 bucket?

Android 如何将图片上传到AWS s3 bucket?,android,amazon-s3,aws-sdk,Android,Amazon S3,Aws Sdk,这是我基于Android s3文档所做的,我正在尝试从照片库中挑选照片,或者从手机中拍摄照片,然后将其上传到s3 bucket,并将其检索回应用程序界面。但每次我运行程序时,我的应用程序都会崩溃。我打电话 //getting access to the photo gallery @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivit

这是我基于Android s3文档所做的,我正在尝试从照片库中挑选照片,或者从手机中拍摄照片,然后将其上传到s3 bucket,并将其检索回应用程序界面。但每次我运行程序时,我的应用程序都会崩溃。我打电话

//getting access to the photo gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        uploadImage.setImageURI(selectedImage);
    }
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();

}


 //Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
//Trying to upload image to the AWS S3bucket-
public void uploadImageToAWS(View v) {
//String sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "Camera/";
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
    File MY_FILE = new File("myPhoto.png");
    String YOUR_IDENTITY_POOL_ID ="POOL_ID";
    CognitoCachingCredentialsProvider credentialsProvider =
            new CognitoCachingCredentialsProvider(getApplicationContext(),
                    YOUR_IDENTITY_POOL_ID,
                    Regions.US_EAST_1);


    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));

    TransferUtility transferUtility = new TransferUtility(s3Client,
            getApplicationContext());

    TransferObserver observer = transferUtility.upload(
            "apptestingbucket",           // The S3 bucket to upload to
            FILE_NAME,   // The key for the uploaded object
            MY_FILE);// The location of the file to
    observer.setTransferListener(new TransferListener() {

        @Override
        public void onStateChanged(int id, TransferState state) {
            if(state == TransferState.COMPLETED){
                Toast.makeText(Social.this, "transfer Succeded!", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent/bytesTotal * 100);
            Log.e("Status", "Percentage" + percentage);

        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("MY_BUCKET", "Error: " + ex.getMessage() );

        }
    });




        }



private void retrieveImageFromAWS(){
    File sdcard = Environment.getExternalStorageDirectory();
    File MY_FILE = new File(sdcard, "social.png");
    String YOUR_IDENTITY_POOL_ID ="POOL_ID";
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(),
            YOUR_IDENTITY_POOL_ID
            , Regions.US_EAST_1);
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    TransferUtility transferUtility = new TransferUtility(s3Client,
            getApplicationContext());
    TransferObserver observer = transferUtility.download("BUCKET_NAME",
            "FILENAME_TO_BE_DOWNLOADED", MY_FILE);

    observer.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            if(state == TransferState.COMPLETED){
                Toast.makeText(Social.this, "Retrieve completed", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            int percentage = (int) (bytesCurrent/bytesTotal * 100);
            Log.e("status", "percentage" +percentage);

        }

        @Override
        public void onError(int id, Exception ex) {
            Log.e("MY_BUCKET", "Error Downloading: " + ex.getMessage());


        }
    });


}
插入

  uploadImageToAWS(v)

任何例子或很少的帮助都将不胜感激。谢谢大家!

尝试检查您的Android清单文件中是否有使用Internet的权限。如果这不起作用,请尝试查看是否可以将应用程序外部的图像(例如,通过命令行)上载到bucket。可能有某种安全设置您没有正确设置。确保您可以ping服务器并正确设置安全设置。

1)如果程序崩溃,请添加logcat 2)您可能需要一个AsyncTask来执行任何网络操作
 BtnUpload.onClicklistener