Mark J
Mark J

Reputation: 31

How to get URI from Android App Incoming Intent

I have read this standard description from Google: https://developer.android.com/training/sharing/receive

Following this, I have updated my AndroidManifest.xml to include an intent-filter to accept share from other apps (for a variety of mime types).

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="SnapLinx Share"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
            <data android:mimeType="video/*" />
            <data android:mimeType="audeo/*" />
            <data android:mimeType="application/*" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SignInActivity"
        android:label="@string/title_activity_sign_in"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name=".SignUpActivity"
        android:label="SignUpActivity"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name=".ForgotPasswordActivity"
        android:label="ForgotPasswordActivity"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="SnapLinx Share"
        android:theme="@style/AppTheme">
    </activity>

    <service
        android:name=".BLEScanIntentSvc"
        android:enabled="true"
        android:exported="false">
    </service>
    <service
        android:name=".NotificationIntentSvc"
        android:exported="false">
    </service>
</application>

I am able to see my app as a share option within other apps and when I select a file within other apps (e.g. an image file within the Photos app) and select the option to share with my app, my app is launched just fine. To start with, I am trying to simply log share parameters within my MainActivity onCreate(). See this code:

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

    / First try at getting Uri obj.
    Uri FileURI = intent.getData();

    if (Intent.ACTION_SEND.equals(action) && type != null)
    {
        Log.i(LOGTAG, "File intent. Type: " + type);
        Log.i(LOGTAG, "File intent. Action: " + action);


        FileURI = (Uri) intent.getParcelableExtra(Intent.EXTRA_TEXT);
        if (FileURI != null)
        {
            Log.i(LOGTAG, "File intent. URL: " + FileURI.toString());
        }
    }

The action and type strings retrieved ("android.intent.action.SEND" and "image/*") are correct, but I have no way of getting the Uri of the file that I had shared from the Photo app. intent.getData() and intent.getParcelableExtra(Intent.EXTRA_TEXT) and intent.getParcelableExtra(Intent.EXTRA_STREAM) always return null.

If I break in the debugger, all of the intent attributes (that would expect to support Uri access) look to be null (i.e. mCatagories=null, mClipData=null mData=null, mExtras=null).

The flags are indicated by mFlags=524288 .... not sure if that helps.

I am building with compile SDK and target SDK both at 28 with min SDK at 23.

My test device is a Galaxy S7 running 8.0.0.

Any advice is appreciated.

Mark J

Upvotes: 2

Views: 2342

Answers (1)

elham
elham

Reputation: 1

Try using Intent.EXTRA_STREAM instead of Intent.EXTRA_TEXT. Like this (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);

Upvotes: 0

Related Questions