5

I am making an app that is able to upload single or multiple files or folders. The intent-filter is defined like this:

    <activity android:name=".UploadActivity" android:launchMode="singleTop" android:theme="@style/Theme.Sherlock">
        <intent-filter android:label="@string/upload_intent">
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:mimeType="application/*" />
            <data android:mimeType="audio/*" />
            <data android:mimeType="image/*" />
            <data android:mimeType="media/*" />
            <data android:mimeType="multipart/*" />
            <data android:mimeType="text/*" />
            <data android:mimeType="video/*" />
        </intent-filter>
        <intent-filter android:label="@string/upload_intent">
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>

This works great with Blackmoon File Explorer and the Android 4.0.3 Gallery. My app shows up in the Share menus. However, it also shows in the Browser, when you long-press the URL and choose to Share Page.

When I send a text file to my app, I get this:

getIntent().getAction() returns Intent.ACTION_SEND
getIntent().getType() returns "text/plain"
getIntent().getScheme() returns null
getIntent().getExtras().getString(Intent.EXTRA_STREAM) returns the filename

However, sending a URL from the Browser returns the same thing minus the Intent.EXTRA_STREAM. I can detect this easily in code and say "hey I can't upload text!" but I'd rather change the intent filter so that it only triggers when there is an EXTRA_STREAM. Is it possible to do so?

(I tried using android:scheme but that won't differentiate between a file and a text string being shared, since they are both null...)

albnok
  • 521
  • 2
  • 14

2 Answers2

3

You have:

getIntent().getExtras().getString(Intent.EXTRA_STREAM) returns the filename

AFAIK, Android never sends you an open fh or socket to a file. Just the plain, old path to the file, which you then handle with:

File somefile = new File(filename);

And then read it or write to it or what not.

As for the incoming text being EXTRA_TEXT or STREAM, just test for both and handle appropriately. Hope this helps.

R Earle Harris
  • 985
  • 9
  • 17
0

I registered for file in the manifest:

<activity android:name=".ReaderActivity">
    <intent-filter >
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

And then used getData() and a content resolver in my activity.

Uri uri = getIntent().getData();
InputStream inputStream = getContentResolver().openInputStream(uri);
...

See this answer to find out what the ... is.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393