7

我在尝试使用 amazon S3 android 库上传用相机拍照时获得的图像时遇到问题。

保存图片

File _photoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),   (cal.getTimeInMillis() + ".jpg"));

            try {
                if (_photoFile.exists() == false) {
                    _photoFile.getParentFile().mkdirs();
                    _photoFile.createNewFile();
                }

            } catch (IOException e) {
                // Log.e(TAG, "Could not create file.", e);
            }
            // Log.i(TAG, path);

            filePath = Uri.fromFile(_photoFile);
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, filePath);
            startActivityForResult(cameraIntent, 1);

使用 filePath 上传图片:

try {
            s3Client.createBucket(Constants.getPictureBucket());

            // Content type is determined by file extension.
            PutObjectRequest por = new PutObjectRequest(
                    Constants.getPictureBucket(), Constants.PICTURE_NAME,
                    new java.io.File(filePath));
            s3Client.putObject(por);
        } catch (Exception exception) {

            result.setErrorMessage(exception.getMessage());
        }

我不断收到错误Unable to calclualte MD5 hash:/file:/storage/sdcard0/DCIM/13161272646580.jpg open failed:ENOENT (No such file or directory)

但是当我浏览我的sd卡目录时,我可以在那里找到图片(图片已创建),并且我已经创建了相关权限。

4

2 回答 2

2

问题出在您的 文件路径中。使用类似的方法来获取真实路径。

我给你举个例子,如果你从 onActivityResult() 获取图像,你应该有一个 onActivityResult(int requestCode, int resultCode, Intent data) ,你可以在其中获取图像的 Uri。

uri uri = data.getData();

然后使用它并从 Uri 获取真实路径并将其传递给 PutObjectRequest(BUCKET_NAME, IMAGE_NAME, REAL_PATH);

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

我有同样的问题,我解决了这个问题。希望能帮助到你!

于 2014-09-17T07:15:19.007 回答
1

我真的不记得我做了什么。但这是我使用的有效代码。希望对大家有帮助!

AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials("XXX", "XXX"));
String time = "" + System.currentTimeMillis();
PutObjectRequest por = new PutObjectRequest("fraternity", xyz_url_xyz + "/avatar" + time + ".jpg", new java.io.File(params[0]));
por.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(por);
ResponseHeaderOverrides override = new ResponseHeaderOverrides();
override.setContentType("image/jpeg");
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest("xyz_url_xyz/avatar.jpg");
urlRequest.setExpiration(new Date(System.currentTimeMillis() + 3600000));  
urlRequest.setResponseHeaders( override );
s3Client.generatePresignedUrl(urlRequest);
于 2014-09-01T16:36:48.547 回答