【问题标题】:Android receive share text file but not share text contentAndroid接收共享文本文件但不共享文本内容
【发布时间】:2012-03-20 20:34:40
【问题描述】:

我正在制作一个能够上传单个或多个文件或文件夹的应用程序。意图过滤器的定义如下:

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

这非常适用于 Blackmoon 文件资源管理器和 Android 4.0.3 Gallery。我的应用程序显示在共享菜单中。但是,当您长按 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

但是,从浏览器发送 URL 返回相同的内容减去 Intent.EXTRA_STREAM。我可以在代码中轻松检测到这一点并说“嘿,我无法上传文本!”但我宁愿更改意图过滤器,使其仅在有 EXTRA_STREAM 时触发。有可能吗?

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

【问题讨论】:

  • 我也想只用 EXTRA_STREAM 触发。你解决了吗?
  • 我也很感兴趣。你是怎么解决这个问题的??

标签: android text share intentfilter extra


【解决方案1】:

你有:

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

AFAIK,Android 永远不会向您发送打开的 fh 或套接字到文件。只是文件的普通旧路径,然后您可以处理:

File somefile = new File(filename);

然后读取或写入或不写入。

对于传入的文本是EXTRA_TEXT 或STREAM,只需测试两者并适当处理即可。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我在清单中注册了file

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

    然后在我的活动中使用getData() 和内容解析器。

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

    查看this answer 了解... 是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      相关资源
      最近更新 更多