如何在Android中使用Java将图像上传到Amazon s3存储桶?

时间:2017-09-11 07:14:23

标签: java android amazon-web-services amazon-s3

我正在尝试将我从相机拍摄的图像上传到s3存储桶。

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"us-west-2:xxxxxxxxxxxxx",
Regions.US_WEST_2
);

 AmazonS3Client s3Client = new AmazonS3Client(new AWSCredentials() {
        @Override
        public String getAWSAccessKeyId() {
            return "XXXXXXX";
        }

        @Override
        public String getAWSSecretKey() {
            return "XXXXXXX";
        }
    });


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

transferUtility.upload(MY_BUCKET,file.getName(),file);

我的游泳池位于us-west-2,但我的水桶区位于亚太地区的悉尼。

运行上面的代码会崩溃,并在下面给出错误。

Java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.amazonaws.mobileconnectors.s3.transferutility.TransferService$NetworkInfoReceiver@744215
                                                                                 at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_50043(LoadedApk.java:1282)
                                                                                 at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.$m$0(Unknown Source:4)
                                                                                 at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.run(Unknown Source:0)
                                                                                 at android.os.Handler.handleCallback(Handler.java:769)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                              Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10084 nor current process has android.permission.ACCESS_NETWORK_STATE.
                                                                                 at android.os.Parcel.readException(Parcel.java:1948)

我在清单文件中添加了权限和服务。我怎样才能解决这个问题并将图像发送到亚太地区悉尼的s3水桶。

我在清单文件下有这些权限 -

   <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE" />

    

1 个答案:

答案 0 :(得分:0)

由于错误明确指出,您需要在Manifest中添加android.permission.ACCESS_NETWORK_STATE权限