ActivityNotFoundException when starting android.in

2019-08-07 12:01发布

问题:

I am running context.startActivity(intent); and my intent looks like this:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(Uri.parse(uri), mimeType);

uri at that point is a String http://remote.server.path/photo-12212324324.jpg

and mimeType is image/jpeg

Here's an example of the exception.

12-07 20:48:55.934: I/ActivityManager(633): START u0 {act=android.intent.action.VIEW dat=http: typ=image/jpeg flg=0x1} from pid 1414
12-07 20:48:55.964: W/dalvikvm(1414): threadid=1: thread exiting with uncaught exception (group=0x40cb5ba0)
12-07 20:48:56.034: E/AndroidRuntime(1414): FATAL EXCEPTION: main
12-07 20:48:56.034: E/AndroidRuntime(1414): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http: typ=image/jpeg flg=0x1 }
12-07 20:48:56.034: E/AndroidRuntime(1414):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1760)
12-07 20:48:56.034: E/AndroidRuntime(1414):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1555)
12-07 20:48:56.034: E/AndroidRuntime(1414):     at android.app.Activity.startActivityForResult(Activity.java:3431)

This happens on at least two devices in the field, one of which is Samsung Galaxy S5, and therefore, I doubt that there's no app to handle the intent as the user will have at least Samsung Gallery app installed.. I know I can just handle the exception and show an error, but that's not really a fix..

回答1:

I am running context.startActivity(intent); and my intent looks like this:

Um, FLAG_GRANT_READ_URI_PERMISSION is useless here. That is only for when your Uri is coming from a ContentProvider.

I doubt that there's no app to handle the intent as the user will have at least Samsung Gallery app installed

The device may have that app. The user may not. Please bear in mind that on Android 4.2+ tablets and Android 5.0+ phones, a device may have multiple users. Some of those users may be "restricted accounts", where they have access only to a whitelisted set of apps.

Please do not assume that all users will have access to an app that can download and view images.

Also, I would not assume that whatever "Samsung Gallery app" is will necessarily support http as a scheme.

I know I can just handle the exception and show an error, but that's not really a fix

Implement your own image viewer as a fallback, if you do not want to show an error.



回答2:

Performing this check is important because if you call startActivity() using an intent that no app can handle, your app will crash. So as long as the result is not null, it's safe to use the intent.

    if (intent.resolveActivity(getPackageManager()) != null) {
        context.startActivity(intent);
    }else{ Toast.makeToast(context, "No application found on device to open view", Toast.LENGTH_SHORT).show()}