Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kitkat, error while trying to load an image

Tags:

android

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" />
like image 486
Pablo Pantaleon Avatar asked Nov 27 '13 16:11

Pablo Pantaleon


1 Answers

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.

like image 125
Pablo Pantaleon Avatar answered Oct 19 '22 11:10

Pablo Pantaleon