Search code examples
androidandroid-intentscreenshotintentfilter

Can't get URI of captured screenshot from intent.getParcelableExtra(Intent.EXTRA_STREAM)!


I'm trying to get the URI of just captured screenshot as I choose my app from Open With dialog. But I always get null from intent.getParcelableExtra(Intent.EXTRA_STREAM) in the provided code sample.

This is my intent filter:

Implemented two intent-filters

First one: Makes my activity main and launcher.

Second one: Makes it an image viewer.(registers this activity on the system as an image viewer)

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="image/*" />
</intent-filter>

And this is how I'm trying to get the URI from the intent that invoked my activity.

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_VIEW.equals(action) && type != null) {
    if (type.startsWith("image/")) {
        Uri mediaUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
        // Here mediaUri is always null 
    }
}

Solution

  • Quoting the documentation for ACTION_VIEW:

    Input: getData() is URI from which to retrieve data.

    So, change your code to:

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    
    if (Intent.ACTION_VIEW.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            Uri mediaUri = intent.getData();
        }
    }