open failed:ENOENT (No such file or directory) error

I've got a problem trying to upload an image I get from taking a picture with the camera,using amazon S3 android library.

To save the picture

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

To upload the picture with the 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());
        }

I keep getting an error Unable to calclualte MD5 hash:/file:/storage/sdcard0/DCIM/13161272646580.jpg open failed:ENOENT (No such file or directory)

but when I browse my sd card's directory I can find the picture there(the picture has been created), and I've created the relevant permissions.


The problem is in your file path . Use a method like that to get the Real Path.

I give you an example, if you are getting the image from onActivityResult() You should have a onActivityResult(int requestCode, int resultCode, Intent data) where you can get the Uri of your image.

Uri uri = data.getData();

Then you use it and get the real path from Uri and pass this to the 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();
        }
    }
}

I had the same problem and I solve it doing this. Hope it helps!


I don't really remember what I did. But this is the code I use that works. Hope it can help you guys!

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);
链接地址: http://www.djcxy.com/p/11804.html

上一篇: 拆分并连接字符串数据的多个逻辑“分支”

下一篇: 打开失败:ENOENT(没有这样的文件或目录)错误