iPaulPro / aFileChooser

[DEPRECATED] Android library that provides a file explorer to let users select files on external storage.
Apache License 2.0
1.79k stars 850 forks source link

java.lang.IllegalArgumentException: Unknown URI #101

Open DipeshGeriya opened 5 years ago

DipeshGeriya commented 5 years ago

java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/2339 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165) android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) android.content.ContentProviderProxy.query(ContentProviderNative.java:421) android.content.ContentResolver.query(ContentResolver.java:532) android.content.ContentResolver.query(ContentResolver.java:473) com.ipaulpro.afilechooser.utils.FileUtils.getDataColumn(FileUtils.java:227) com.ipaulpro.afilechooser.utils.FileUtils.getPath(FileUtils.java:298)

DipeshGeriya commented 5 years ago

when i selecting file from recent tab, facing this issue. please help me.

arunarajput commented 5 years ago

I'm also getting the same issue. Actually this library is not supporting in Android 8+. Still I'm finding a solution. Did you get any solution??

sumanabhi commented 5 years ago

Did you guys find a solution or not?? I am facing the same issues

arunarajput commented 5 years ago

@abhi08singh I didnt get any solution. I'm not getting what is the issue. @iPaulPro please help us

huunhan18pro commented 5 years ago

Please check it.

public static final String DOCUMENTS_DIR = "documents";

 if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);

                if (id != null && id.startsWith("raw:")) {
                    return id.substring(4);
                }

                String[] contentUriPrefixesToTry = new String[]{
                        "content://downloads/public_downloads",
                        "content://downloads/my_downloads"
                };

                for (String contentUriPrefix : contentUriPrefixesToTry) {
                    Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
                    try {
                        String path = getDataColumn(context, contentUri, null, null);
                        if (path != null) {
                            return path;
                        }
                    } catch (Exception e) {}
                }

                // path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams
                String fileName = getFileName(context, uri);
                File cacheDir = getDocumentCacheDir(context);
                File file = generateFileName(fileName, cacheDir);
                String destinationPath = null;
                if (file != null) {
                    destinationPath = file.getAbsolutePath();
                    saveFileFromUri(context, uri, destinationPath);
                }

                return destinationPath;
            }

 public static String getFileName(@NonNull Context context, Uri uri) {
        String mimeType = context.getContentResolver().getType(uri);
        String filename = null;

        if (mimeType == null && context != null) {
            String path = getPath(context, uri);
            if (path == null) {
                filename = getName(uri.toString());
            } else {
                File file = new File(path);
                filename = file.getName();
            }
        } else {
            Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (returnCursor != null) {
                int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                returnCursor.moveToFirst();
                filename = returnCursor.getString(nameIndex);
                returnCursor.close();
            }
        }

        return filename;
    }

    public static String getName(String filename) {
        if (filename == null) {
            return null;
        }
        int index = filename.lastIndexOf('/');
        return filename.substring(index + 1);
    }

    public static File getDocumentCacheDir(@NonNull Context context) {
        File dir = new File(context.getCacheDir(), DOCUMENTS_DIR);
        if (!dir.exists()) {
            dir.mkdirs();
        }
//        logDir(context.getCacheDir());
//        logDir(dir);

        return dir;
    }

    @Nullable
    public static File generateFileName(@Nullable String name, File directory) {
        if (name == null) {
            return null;
        }

        File file = new File(directory, name);

        if (file.exists()) {
            String fileName = name;
            String extension = "";
            int dotIndex = name.lastIndexOf('.');
            if (dotIndex > 0) {
                fileName = name.substring(0, dotIndex);
                extension = name.substring(dotIndex);
            }

            int index = 0;

            while (file.exists()) {
                index++;
                name = fileName + '(' + index + ')' + extension;
                file = new File(directory, name);
            }
        }

        try {
            if (!file.createNewFile()) {
                return null;
            }
        } catch (IOException e) {
            //Log.w(TAG, e);
            return null;
        }

        //logDir(directory);

        return file;
    }

    private static void saveFileFromUri(Context context, Uri uri, String destinationPath) {
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            is = context.getContentResolver().openInputStream(uri);
            bos = new BufferedOutputStream(new FileOutputStream(destinationPath, false));
            byte[] buf = new byte[1024];
            is.read(buf);
            do {
                bos.write(buf);
            } while (is.read(buf) != -1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
arunarajput commented 5 years ago

@huunhan18pro where is the declaration on getDataColumn()?

huunhan18pro commented 5 years ago

@huunhan18pro where is the declaration on getDataColumn()?

https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java

tamimattafi commented 4 years ago

@huunhan18pro after searching everywhere, you saved my life!

chandrikaReddyBunga commented 3 years ago

LocalStorageProvider whre u menciton that