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)