Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

java.lang.IllegalArgumentException: Unknown URI #101

Open
DipeshGeriya opened this issue Oct 25, 2018 · 9 comments
Open

java.lang.IllegalArgumentException: Unknown URI #101

DipeshGeriya opened this issue Oct 25, 2018 · 9 comments

Comments

@DipeshGeriya
Copy link

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
Copy link
Author

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

@arunarajput
Copy link

arunarajput commented Oct 31, 2018

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
Copy link

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

@arunarajput
Copy link

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

@huunhan18pro
Copy link

huunhan18pro commented Nov 15, 2018

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
Copy link

@huunhan18pro where is the declaration on getDataColumn()?

@huunhan18pro
Copy link

@tamimattafi
Copy link

@huunhan18pro after searching everywhere, you saved my life!

@chandrikaReddyBunga
Copy link

LocalStorageProvider whre u menciton that

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants