Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New KitKat URIs dont respond to Intent.ACTION_VIEW

Since KitKat has changed the URIs from pickers to likes of

 content://com.android.providers.media.documents/document/image:3951

then none of my ACTION_VIEW intents work anymore. When for example user picks an image, I use

public static void openImage(Fragment f, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/*");

        f.startActivity(intent);
    }

and Android Gallery and Google+ Photos come up, but when selected, the Gallery just shows blank screens, Photos says "medium not found"

The same thing with sounds, I am used to use

public static void playSound(Fragment f, Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "audio/*");

    f.startActivity(intent);
}

Which used to show Google Play Music in previous versions, with the little white playing dialog UI. With the new URIs, I get exception that no app is able to handle this intent.

// With photos, the funny thing is that, when you choose Gallery instead of the Pictures in the new KK picker UI, it returns the old URIs which work.

Any ideas? Are the system apps just not ready for the new uris? Should I somehow hack the new uris to old ones to make intents work? Or am I missing something?

Thanks!

like image 887
urSus Avatar asked Feb 19 '14 16:02

urSus


1 Answers

The solution is to pass the flag FLAG_GRANT_READ_URI_PERMISSION to the intent (on KitKat and above):

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Also make sure that this content Uri was retrieved from an ACTION_OPEN_DOCUMENT intent as described here: https://stackoverflow.com/a/19874645/334209

like image 192
Romain Piel Avatar answered Oct 15 '22 15:10

Romain Piel