Android接收共享文本文件但不共享文本内容

alb*_*nok 5 android text share intentfilter extra

我正在制作一个能够上传单个或多个文件或文件夹的应用程序.intent-filter的定义如下:

    <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>
Run Code Online (Sandbox Code Playgroud)

这适用于Blackmoon文件资源管理器和Android 4.0.3图库.我的应用程序显示在"共享"菜单中.但是,它也会在浏览器中显示,当您长按URL并选择共享页面时.

当我将文本文件发送到我的应用程序时,我得到了这个:

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

但是,从浏览器发送URL会返回相同的内容减去 Intent.EXTRA_STREAM.我可以在代码中轻松检测到这一点并说"嘿,我无法上传文字!" 但我宁愿改变意图过滤器,以便它只在有EXTRA_STREAM时触发.有可能这样做吗?

(我尝试使用android:scheme但不会区分文件和共享的文本字符串,因为它们都是null ...)

R E*_*ris 3

你有:

getIntent().getExtras().getString(Intent.EXTRA_STREAM) returns the filename
Run Code Online (Sandbox Code Playgroud)

AFAIK,Android 永远不会向您发送打开的 fh 或套接字到文件。只是文件的简单旧路径,然后您可以使用它进行处理:

File somefile = new File(filename);
Run Code Online (Sandbox Code Playgroud)

然后读取它或写入它或诸如此类。

至于传入的文本是EXTRA_TEXT还是STREAM,只需测试两者并进行适当处理即可。希望这可以帮助。