Pablo Pantaleon
Pablo Pantaleon

Reputation: 1536

Kitkat, error while trying to load an image

I'm trying to load an image.. wel this works well when i using API 18 or lower, but if i use API 19 (Kitkat) a problem of security ocurrs... i'm not sure how to solve this, but the cursorloader throught me an exception. I read somenting like

http://developer.android.com/guide/topics/providers/document-provider.html#client

but without a good result. If you saw below i've a new path about the image file, so i can manage this?

*STACK_TRACE=java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%3A1384 from pid=3632, uid=10027 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()*

CursorLoader cursorLoader = new CursorLoader(
                context,
                uri, 
                projection,
                null, null, null);

Some permissions that i've in manifest

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
    <uses-permission android:name="com.android.vending.BILLING" />

Upvotes: 14

Views: 12020

Answers (3)

feri
feri

Reputation: 79

It might depend on where your uri comes from. If the Uri was returned from the file picker, all should work without exceptions.

If you build the Uri by yourself or have persisted the Uri, then there can be problems.

This is inherent in the new Storage Access Framework introduced with Android 4.4 KitKat.

If the user selects files for opening in the file picker, the system grants permission for exactly theese files to your app. Theese permissions are valid as long as the device is not rebootet.

If the Uri is persisted, then the rights granted to access the Uri must also be persisted. For more details look here: https://developer.android.com/guide/topics/providers/document-provider.html#permissions

Upvotes: 3

Pablo Pantaleon
Pablo Pantaleon

Reputation: 1536

Well exist a simple solution for this:

    public static final String KITKAT_VALUE = 1002;

Intent intent;

        if (Build.VERSION.SDK_INT < 19){
                                intent = new Intent();
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.setType("image/*");
                                startActivityForResult(intent, KITKAT_VALUE);
                            } else {
                                intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                                intent.addCategory(Intent.CATEGORY_OPENABLE);
                                intent.setType("image/*");
                                startActivityForResult(intent, KITKAT_VALUE);
                        }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == KITKAT_VALUE ) {
     if (resultCode == Activity.RESULT_OK) {
     // do something here
     }
  }
}

NOTE: No is necessary and additional permission to do this (Manifest modifications). Only write this few code and then you will be able to select images from "Downloads" or from "Recent"

i read this post, help me to write this.

Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

also check this too

http://developer.android.com/guide/topics/providers/document-provider.html#client

Thanks finder.

NOTE: This is a temporal solution, i couldn't find a better way to make it works. But for now i can say, problem solved.

Upvotes: 17

lagunex
lagunex

Reputation: 156

I just found this error today.

It is a reported bug for cordova and KitKat. You can find detailed info here https://issues.apache.org/jira/browse/CB-5398

The bug occurs when the pictured selected comes from the new KitKat options "Recent", "Downloads", etc.

If you go all the way down to the Gallery app and open the standard gallery, you shouldn't have any problem.

Upvotes: 5

Related Questions