1

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..

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
vkislicins
  • 3,331
  • 3
  • 32
  • 62

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer. The method supports content provider Uri as well, hence the flag. Gallery app (the default one) supports http, at least the version installed on Samsung S3 and S4. Unfortunately I don't have S5 to test with but I think it's safe to assume it's similar. I agree that in general I shouldn't assume that the user can open the image and will implement my own media viewer, however, I wanted to check if there's something wrong with my code or someone else has seen similar behaviour that I found strange. – vkislicins Dec 08 '15 at 13:07
1

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()}
Ishan
  • 1,172
  • 10
  • 25
  • yes I get that and will wrap the code up to avoid a crash, however that still doesn't explain why Samsung S5 couldn't handle the intent. – vkislicins Dec 08 '15 at 12:54
  • Because S5 device which you are using does not have any application installed to handle the result from your intent. – Ishan Dec 08 '15 at 12:56
  • For example I uninstall all camera application from my phone and fire Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); it will crash – Ishan Dec 08 '15 at 12:58
  • Because it will not find any application to open camera – Ishan Dec 08 '15 at 12:59
  • I don't think you can uninstall Gallery from a stock S5.. or camera. Those apps are fixed in the Samsung build. But it's not my device, it's a device in the field, so I can't be 100% sure – vkislicins Dec 08 '15 at 13:01
  • :) I was giving you an example. It happened with me on micromax device. I was trying to fire intent for cropping image using other application on phone. – Ishan Dec 08 '15 at 13:04
  • if you go through http://developer.android.com/training/camera/photobasics.html, you will find google also recommend to perform the check for same reason. – Ishan Dec 08 '15 at 13:04
  • yeah, absolutely. I appreciate it! – vkislicins Dec 08 '15 at 13:08