Search code examples
androidandroid-sharing

Sharing a file between two specific android apps


I am writing apps A and B. I want A to be able to send B a file URI of any given mime type. I found an explanation of how to handle someone making a request for a file, so more like a pull, where as I want to push a URI from A to B, without B requesting. I have also seen a more generic description of how to share data between apps, but that uses ACTION_SEND and, in that case, the user will be presented with a list of apps to choose from. I only want app B specifically to receive the file URI. In addition, I don't want app B to show up as a choice when someone is doing an ACTION_SEND for a different purpose. So, how do I send a file URI from one specific app to another specific app?

Edit 1: Adding some code snippets to the example.

In app A, I am doing the following:

Intent testIntent = new Intent("com.example.appB.TEST");
testIntent.addCategory(Intent.CATEGORY_DEFAULT);
testIntent.setData(Uri.parse("file:///mnt/sdcard/test.txt"));
startActivityForResult(testIntent, TEST);

In app B, I have the following in the manifest:

<activity
    android:name="com.example.appB.mainActivity"
    android:label="@string/app_name" 
    android:launchMode="singleTask" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.example.appB.TEST" />
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="file"/>
            <data android:mimeType="*/*"/>
    </intent-filter>
</activity>

The error I get is ActivityNotFoundException. If I get rid of the setData line, the activity is launched, but obviously I don't have the URI.

You can read about more issues I hit here.


Solution

  • I think the first three answers provided are all correct ways to make this work. Currently, I am using a small modification of my code. I needed to use setDataAndType and not just setData. Even though app B said it could handle any mimeType, not specifying the mimeType in app A made this not work.

    You can read about more issues I hit here.