2

using Intent.ACTION_GET_CONTENT to open the file picker

    Intent openIntent = new Intent(Intent.ACTION_GET_CONTENT);
    openIntent.addCategory(Intent.CATEGORY_OPENABLE);
    String contentType = "*/*";
    openIntent.setType(contentType);
    startActivityForResult(openIntent, ANDROID_FILE_PICKER);

and from there in ‘downloads’ selected one file, the uri it returns:

content://com.android.providers.media.documents/document/image%3A1679

when using the uri to query the file's data it returns fine. but using same uri to do openInputStream(uri), it throws "FileNotFoundException: No such file or directory".

when pick up other file it returns uri:

content://com.android.providers.media.documents/document/image%3A3372

which works fine with query and openInputStream.

is it possible the android returned uri might for the query, but cannt be used for openInputStream (someone works though)? what could cause this? is there another way to get the file's content?

code snippet:

try {
  cursor = context.getContentResolver().query(uri, null, null, null, null);
  if (cursor != null && cursor.moveToFirst()) {
     fileDisplayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

     int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
     if (!cursor.isNull(sizeIndex)) {
         fileSize = cursor.getInt(sizeIndex);
     }

     if (!TextUtils.isEmpty(fileDisplayName) && fileSize > 0 {
        // the query uri return cursor with the filename and size, looks good
        try {

           // it throws at this line:
           InputStream inputStream = context.getContentResolver().openInputStream(uri);

                   ... ...

        } catch (Exception ex) {
           //java.io.FileNotFoundException: No such file or directory 
        }
     }
  }

} catch (Exception e) {
    return null;
} finally {
   if (cursor != null) {
      cursor.close();
   }
}

the call stack:

05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err: java.io.FileNotFoundException: No such file or directory
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at com.zeta.app.utils.FileUtils.cacheFileLocally(FileUtils.java:63)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at com.zeta.app.ui.FilePickerActivity.pickedFile(FilePickerActivity.java:377)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at com.zeta.app.ui.FilePickerActivity.access$100(FilePickerActivity.java:84)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at com.zeta.app.ui.FilePickerActivity$1.run(FilePickerActivity.java:243)
05-17 18:25:41.816 28620-30341/com.zeta.app W/System.err:     at java.lang.Thread.run(Thread.java:818)
lannyf
  • 9,865
  • 12
  • 70
  • 152
  • Please edit your question and post the entire Java stack trace. Also, to confirm: is this code in the same activity that is getting the `Uri` from `onActivityResult()`? And, are you sure that the first piece of content actually exists? Bear in mind that the `MediaStore` can lag behind what is on the filesystem, if the file has been deleted and the `MediaStore` index has not been updated yet to reflect that fact. – CommonsWare May 17 '17 at 21:51
  • Thanks CommonWare!, callstack is added, it is from the same onActivityResult(). I have been tested against the same file multiple times. Cannt explain why openStream on that uri will throw even though the query with the uri is fine. – lannyf May 17 '17 at 22:37
  • 1
    This really feels like the underlying file is missing. – CommonsWare May 17 '17 at 22:40
  • yeah, I would agree. But how the file picker displays it and the query returns the cursor with proper data (the same file has been showing in the picker dont remember since when, and the device has been re-booted a few times). – lannyf May 18 '17 at 11:51

1 Answers1

0

Make sure you have the "Access External Storage" permission in you Manifest Class within "Application" Block. And if you are using Android version 23 or 23+, then you have to ask for permission at run-time.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • it does have all the permissions, and if picking up some other file it is working fine but just someone will get this exception. – lannyf May 17 '17 at 22:38
  • Then make sure the file you are trying to access exists by "yourfile.isExists()" method and also make sure you are providing the right path for the file. – Zohaib Hassan May 17 '17 at 22:46