Intent Filter for app in share menu - only show if LOCAL files are shared

472 views Asked by At

I want my app to only show up in the share menu if local available files are shared.

Currently it looks like following, but this shows my app whenever images are shared, no matter if the are from a internal content provider or if paths to real files on the file system are shared.

 <activity
        android:name=".activities.ViewerActivity"
        android:label="@string/app_name_sharing_to_display"
        android:theme="@style/AppThemeFullScreen"
        android:launchMode="singleTask" >
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE"/>
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*"/>
        </intent-filter>
    </activity>

I'm receiving the local path like following and I save them in a history, I don't want nor need other files.

private String getRealPathFromURI(Uri contentURI)
{
    String result = null;
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null)
    {
        result = contentURI.getPath();
    }
    else
    {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        if (idx >= 0)
            result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}
1

There are 1 answers

12
CommonsWare On

That getRealPathFromURI() will not work even for "local files" on >500 million devices, running Android 4.4 and higher.

First, you may not get Uri values from MediaStore, as there are other ACTION_GET_CONTENT implementations available for Android (e.g., Google Drive, Dropbox), and their contents are not necessarily in MediaStore.

Second, MediaStore does not have to support the DATA column as holding some local filesystem path.

Third, the file may still be inaccessible, if it is on removable storage, for which you have no access. If you are not going to support content:// Uri values using a ContentResolver, your app will not be useful to many people.

That being said, you are welcome to add a <data> element to limit your <intent-filter> to only file schemes.