Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android receive share text file but not share text content

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...)

like image 973
albnok Avatar asked Oct 24 '22 07:10

albnok


1 Answers

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.

like image 77
R Earle Harris Avatar answered Nov 11 '22 11:11

R Earle Harris