我在测试手机中收到了“没有应用可以执行此操作”的消息,但在平板电脑中,它运行正常。我的测试手机是Android版本4.2.2,我的平板电脑是Android版本4.4.4。这是我的代码。
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/pdf","text/plain","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Select File"), 3);
如果我换了这样的话,
startActivityForResult(intent, 3);
我的手机出现了这个错误,但平板电脑仍然没问题。
03-12 09:22:40.015: E/test(10739): Exception
03-12 09:22:40.026: E/AndroidRuntime(10739): FATAL EXCEPTION: main
03-12 09:22:40.026: E/AndroidRuntime(10739): java.lang.IllegalStateException: Could not execute method of the activity
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.view.View$1.onClick(View.java:3688)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.view.View.performClick(View.java:4293)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.view.View$PerformClick.run(View.java:17535)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.os.Handler.handleCallback(Handler.java:725)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.os.Looper.loop(Looper.java:153)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.ActivityThread.main(ActivityThread.java:5299)
03-12 09:22:40.026: E/AndroidRuntime(10739): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 09:22:40.026: E/AndroidRuntime(10739): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 09:22:40.026: E/AndroidRuntime(10739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-12 09:22:40.026: E/AndroidRuntime(10739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-12 09:22:40.026: E/AndroidRuntime(10739): at dalvik.system.NativeStart.main(Native Method)
03-12 09:22:40.026: E/AndroidRuntime(10739): Caused by: java.lang.reflect.InvocationTargetException
03-12 09:22:40.026: E/AndroidRuntime(10739): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 09:22:40.026: E/AndroidRuntime(10739): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.view.View$1.onClick(View.java:3683)
03-12 09:22:40.026: E/AndroidRuntime(10739): ... 11 more
03-12 09:22:40.026: E/AndroidRuntime(10739): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT cat=[android.intent.category.OPENABLE] typ=*/* (has extras) }
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity.internalStartActivityForResult(Activity.java:3427)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity.access$200(Activity.java:660)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity$2.onStartActivity(Activity.java:3417)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity.startActivityForBusiness(Activity.java:5441)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity.startActivityForResult(Activity.java:3413)
03-12 09:22:40.026: E/AndroidRuntime(10739): at android.app.Activity.startActivityForResult(Activity.java:3367)
这应该适用于两种设备,我该怎么办。请帮助我。!!
答案 0 :(得分:2)
仅在安装了Android 4.4(KitKat)及更高版本的设备上支持Intent Intent.ACTION_OPEN_DOCUMENT
。
查看Storage Access Framework文档,特别是ACTION_OPEN_DOCUMENT
上的文档。
答案 1 :(得分:0)
在Android 4.3或更低版本中,使用ACTION_PICK或ACTION_GET_CONTENT作为替代。
此外,当您调用内置" ACTION"时,您必须验证是否有多个活动才能接收它,否则您的应用可能会崩溃。使用以下代码段执行此操作:
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
startActivity(intent);
}