java.io.FileNotFoundException in android
Asked Answered
M

3

7

I'm selecting a image from gallery using code

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, SELECT_PICTURE);
}

public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    Bitmap bitmap = null;
    switch (requestCode) {
    case SELECT_PICTURE:
        if (resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            try {
                bitmap = decodeUri(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            MyApplicationGlobal.bitmap = bitmap;
            MyApplicationGlobal.imageUri = selectedImage.toString();
            Log.v("selectedImage", "selectedImage: " + selectedImage.getPath());

            Intent intentUpload = new Intent(GalleryActivity.this, UploadActivity.class);
            startActivity(intentUpload);
            finish();
        }
    }

then I try to upload that image by calling web service

MultipartEntity reqEntity = new MultipartEntity();
    try {
        reqEntity.addPart("email", new StringBody(email));
        Log.v("in uploadImageUsingMultiPart", "imagePath: " + MyApplicationGlobal.imageUri);
        reqEntity.addPart("name", new FileBody(new File(MyApplicationGlobal.imageUri)));
        reqEntity.addPart("img_desc", new StringBody(img_desc));
        reqEntity.addPart("amount", new StringBody(amount));
        reqEntity.addPart("request_type", new StringBody("INSERT"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

But I'm getting error java.io.FileNotFoundException

Log cat is

 java.io.FileNotFoundException: /content:/media/external/images/media/526 (No such file or directory)
at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
at java.io.FileInputStream.<init>(FileInputStream.java:88)
at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:100)
at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206)
at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224)
at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183)
at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162)
at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.network.GetJSONFomURL.getJSONSrtringFromUrl(GetJSONFomURL.java:53)
at com.network.WebServices.uploadImageUsingMultiPart(WebServices.java:73)
at com.markphoto_activities.UploadActivity$MyAsyncTaskUploadImage.doInBackground(UploadActivity.java:127)
at com.markphoto_activities.UploadActivity$MyAsyncTaskUploadImage.doInBackground(UploadActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
Melton answered 27/12, 2012 at 11:56 Comment(3)
have you set the permission for allow external storage write/read? "java.io.FileNotFoundException: /content:/media/external/images/media/526 (No such file or directory)" you can't read itCrocodilian
@matheszabi I have added this line <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in manifest. Is it enough?Melton
maybe this will help you: google.com/…Crocodilian
C
14

Call this method, pass it the selectedUri, in return you will get the path that you want

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Consciousness answered 27/12, 2012 at 12:15 Comment(3)
wow. this saved my day. thanks. had no way to tell the uri was wrong.Stratovision
I get en exception: storage/emulated/0/Pictures/PicCollage/Photo 2013-12-13 14_33_55.jpg: open failed: EACCES (Permission denied)Shaylyn
@Shaylyn Have you requested android.permission.WRITE_EXTERNAL_STORAGE in your manifest? If you are on API level 23 and above, you also need to implement Run Time Permissions.Outrigger
A
19

Just an update since managedQuery has been deprecated.

private String getPath(Uri uri) {
    String[]  data = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(context, uri, data, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Anaximenes answered 30/1, 2013 at 13:41 Comment(0)
C
14

Call this method, pass it the selectedUri, in return you will get the path that you want

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Consciousness answered 27/12, 2012 at 12:15 Comment(3)
wow. this saved my day. thanks. had no way to tell the uri was wrong.Stratovision
I get en exception: storage/emulated/0/Pictures/PicCollage/Photo 2013-12-13 14_33_55.jpg: open failed: EACCES (Permission denied)Shaylyn
@Shaylyn Have you requested android.permission.WRITE_EXTERNAL_STORAGE in your manifest? If you are on API level 23 and above, you also need to implement Run Time Permissions.Outrigger
R
4

that's because content:/media/external/images/media/526 is not a file. It's an URI address of the media provider. If you want the file check this SO question:

Get filename and path from URI from mediastore

Ran answered 27/12, 2012 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.