启动Intent时权限被拒绝。使用ACTION_GET_CONTENT设置类型(" * / *")

时间:2014-05-16 04:32:03

标签: android android-intent mime-types filepath

使用Aciton_Get_Content。我正在尝试获取链接文件,照片,视频......以供上传。这是我的代码:

Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file to upload "),88);

一切都好。但是一些选择者撞毁了我的应用程序。有了这个,我收到以下错误:

05-16 11:20:18.810: E/AndroidRuntime(15472): FATAL EXCEPTION: main
05-16 11:20:18.810: E/AndroidRuntime(15472): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.GET_CONTENT typ=*/* flg=0x3000000 cmp=xxxx } from ProcessRecord{4373bf30 15472:com.fsharemobile/10119} (pid=15472, uid=10119) not exported from uid 10148
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.os.Parcel.readException(Parcel.java:1327)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.os.Parcel.readException(Parcel.java:1281)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1714)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.app.Activity.startActivityForResult(Activity.java:3195)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.app.Activity.startActivity(Activity.java:3302)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at com.android.internal.app.ResolverActivity.onIntentSelected(ResolverActivity.java:262)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at com.android.internal.app.ResolverActivity.onClick(ResolverActivity.java:176)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:929)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.widget.AbsListView.performItemClick(AbsListView.java:1182)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2710)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.widget.AbsListView$1.run(AbsListView.java:3465)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.os.Handler.handleCallback(Handler.java:605)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.os.Looper.loop(Looper.java:137)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at android.app.ActivityThread.main(ActivityThread.java:4511)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at java.lang.reflect.Method.invokeNative(Native Method)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at java.lang.reflect.Method.invoke(Method.java:511)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
05-16 11:20:18.810: E/AndroidRuntime(15472):    at dalvik.system.NativeStart.main(Native Method)

任何想法,谢谢你的帮助?

1 个答案:

答案 0 :(得分:1)

特别是在 Kitkat 中,您将获得权限拒绝例外,为了解决此问题,您可以执行此操作添加此权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后

Intent intent;
if (Build.VERSION.SDK_INT < 19) {
    intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "Select file to upload "),88);
} else {
    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "Select file to upload "),88);
}