Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

android - 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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...