Xamarin android从图库中选择图像并将其上传到亚马逊s3

时间:2018-08-08 09:55:27

标签: android amazon-s3 xamarin.android android-gallery

我想从图库中拾取图像并将其正确地上传到amazon s3,但是无法将其转换为bitMap图像,并且在将其加载到amazon s3期间无法从其路径读取图像并引发异常:

***Access to the path "/storage/emulated/0/Download/depositphotos_59094043-stock-illustration-profile-icon-male-avatar.jpg" is denied.***

我添加了权限READ_EXTERNAL_STORAGE:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这是选择图像和活动结果的代码:

[Export("SelectImage")]
        public void SelectImage(View view)
        {
            Intent galleryIntent = new Intent(Intent.ActionPick,
                    Android.Provider.MediaStore.Images.Media.ExternalContentUri);
            StartActivityForResult(galleryIntent, RESULT_LOAD_IMG);
        }


        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            try
            {
                if (requestCode == RESULT_LOAD_IMG && resultCode == Result.Ok && null != data)
                {
                    imageData = ProfilesLogic.FetchSelectedImage(this, data);
                    profileImage.SetImageURI(data.Data);

                }
                else
                {
                    Toast.MakeText(this, "You haven't picked Image",
                            ToastLength.Long).Show();
                }

            }
            catch (Exception e)
            {
                Toast.MakeText(this, "Something went wrong", ToastLength.Long);
            }
        }

这是获取图像并从路径获取位图图像的代码返回null:

public static ImageData FetchSelectedImage(Context context, Intent data)
        {
            ImageData imageData = new ImageData();
            Android.Net.Uri selectedImage = data.Data;
            String[] filePathColumn = { MediaStore.Images.Media.InterfaceConsts.Data };
            var cursor = context.ContentResolver.Query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.MoveToFirst();
            int columnIndex = cursor.GetColumnIndex(filePathColumn[0]);
            imageData.imagePath = cursor.GetString(columnIndex);
            File f = new File(imageData.imagePath);
            imageData.imageName = f.Name;

            String imgDecodableString = cursor.GetString(columnIndex);
            cursor.Close();
            string actPath = FilePathHelper.GetActualPathFromFile(selectedImage, context);
            Bitmap bitmap = BitmapFactory.DecodeFile(imgDecodableString);
            imageData.imageBitmap = bitmap;
            return imageData;

        }

将图片上传到亚马逊s3的代码:

public async Task<string> UploadImage(string fileName, string filePath)
        {
            try
            {
                string accessKey = "MYAccessKey";
                string secretKey = "MySecretKey";
                string bucketName = "MyBucketName";
                string folderName = "Android_Uploads"; /

                var amazonS3Config = new AmazonS3Config
                {
                    SignatureVersion = "4",
                    ServiceURL = bucketName,
                    RegionEndpoint = RegionEndpoint.EUCentral1,
                    SignatureMethod = SigningAlgorithm.HmacSHA256
                };
                // Create a client
                AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, amazonS3Config);

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = folderName + "/" + fileName,
                    FilePath = filePath
                };

                // Put object
                var response = await client.PutObjectAsync(request);
                PutObjectResponse res = response;
                return "put url here";
            }
            catch (Exception e)
            {
                return "";
            }
        }

,它会在以下行引发异常:Access to the path "/storage/emulated/0/Download/depositphotos_59094043-stock-illustration-profile-icon-male-avatar.jpg" is denied.

var response = await client.PutObjectAsync(request);

注意:

运行时的图像名称= "depositphotos_59094043-stock-illustration-profile-icon-male-avatar.jpg"

运行时的图像路径= "/storage/emulated/0/Download/depositphotos_59094043-stock-illustration-profile-icon-male-avatar.jpg"

0 个答案:

没有答案
相关问题