Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting IllegalArgumentException during picking a document file from download manager ,which is happening only for oreo

Here I am attaching the logs :

   Caused by: java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/1587
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)

I am using this code which is working fine.But for the case of download manager it is throwing exception at first line of 'try' block

 Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;

I already tried this: Android getting file path from content URI using contentResolver and this: java.lang.IllegalArgumentException: Unknown URI content and some others related to this question but not any one of them resolves my problem.

like image 786
Ajay Chauhan Avatar asked Oct 01 '18 13:10

Ajay Chauhan


People also ask

What is illegalargumentexception in Java?

The IllegalArgumentException is a subclass of java.lang.RuntimeException. RuntimeException, as the name suggests, occurs when the program is running. Hence, it is not checked at compile-time. When a method is passed illegal or unsuitable arguments, an IllegalArgumentException is thrown.

Why do I get exceptions when I open a file?

This can be caused due to several factors like invalid user input, network failure, memory limitations, trying to open a file that does not exist, etc. If an exception occurs, an Exception object is generated, containing the Exception’s whereabouts, name, and type. This must be handled by the program.

What happens if RuntimeException is not handled at compile-time?

If not handled, it gets past to the default Exception handler, resulting in an abnormal termination of the program. The IllegalArgumentException is a subclass of java.lang.RuntimeException. RuntimeException, as the name suggests, occurs when the program is running. Hence, it is not checked at compile-time.


1 Answers

I was getting the same error Unknown URI: content://downloads/public_downloads. I managed to solve this by changing contentUri and by using InputStream methods to fetch file from Download directory. In some of devices changing contentUri to content://downloads/my_downloads works. Checkout this answer for full solution.

like image 114
karanatwal.github.io Avatar answered Nov 01 '22 17:11

karanatwal.github.io