android - 在应用程序之间传输文件 (.txt)

标签 android android-intent share data-import

应用程序在通过它共享时正在导出文本 (.txt) 文件,我可以看到 Gmail、Dropbox、drive、share it 等应用程序通过该应用程序共享它。我想在我的应用程序中导入该文本文件以进一步修改它。 txt文件。 我应该在 list 或主要 Activity 中实现什么? 我应该使用 INTENT 过滤器还是应该使用 getUriForFile() 方法来接收文本文件? 我的应用程序将如何出现在导出文本文件的其他应用程序共享列表中? export form whatsapp send chat via... share text file from file explorer whatsapp extract my app is visible

最佳答案

您可以使用以下 intent-filter 将任何文件从另一个应用程序导入您的 list

            <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="*/*"/>
            <data android:mimeType="text/plain"/>

然后在您的 Activity 中使用以下代码获取文本文件的 URI

    private Uri getUriForFile() {
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    if (TextUtils.equals(Intent.ACTION_SEND, action) && !TextUtils.isEmpty(type)) {

        Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (uri != null) {
            Log.e("uri",uri.toString());
            return  uri;
        }
    }
    return null;
}

UPDATE

如果该 Activity 是您的启动器 Activity ,则使用以下 Intent 过滤器

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="*/*"/>
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>

关于android - 在应用程序之间传输文件 (.txt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268632/

相关文章:

android - 通过 PC (Eclipse) 在物理设备上模拟来电

android - 获取android屏幕的文字内容

java - 如何使用 Intent 设置警报 选择警报的完整日期(包括日、月)

wpf - View 不能被多个 Listview 共享

linux - Powershell查询WINDOWS->LINUX

java - 获取选择的分享意向

android - android中 ListView 项目的边框

android - 动画文件夹没有出现

android - 自定义上下文菜单,如 pinterest 菜单

Android 有时我的辅助 storage.exists() 即使我卸载它也会返回 true