向Android应用程序发送配置文件

时间:2017-05-22 10:08:56

标签: android android-intent

我有一个可以保存/加载配置文件的应用程序(json)

我想发送此文件并将其从设备传送到另一台设备 所以我添加了这种可能性,以便应用程序可以发送到gmail或其他任何

                            Intent shareIntent = new Intent();
                            shareIntent.setAction(Intent.ACTION_SEND);
                            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
                            shareIntent.setDataAndType(fileUri, getContentResolver().getType(fileUri));
                            shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
                            startActivity(Intent.createChooser(shareIntent, "Choose an app"));

我添加到清单:

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

编辑:我也在清单中:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.xxxx.yyyyy.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>

我添加了下面的行来处理意图

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
         //????
            Toast.makeText(getApplicationContext(), "Receive the file", Toast.LENGTH_SHORT).show();
        }
    }

它甚至没有建议我将我的应用程序作为可用于打开文件的应用程序。 为什么呢?

但如果我分享一些文字(来自Viber或......),我会看到它进入代码寻找&#34; Text / *&#34;

我需要的是建议我的应用程序作为文本文件的处理程序

2 个答案:

答案 0 :(得分:0)

您已在intent过滤器中将“text / plain”设置为MIME类型,因此您必须将shareIntent的类型设置为“text / plain”。

换句话说,请确保getContentResolver().getType(fileUri)确实返回“text / plain”,这可能与您的代码不同。

因此,如果按照以下方式致电setDataAndType,那就更好了。

shareIntent.setDataAndType(fileUri, "text/plain");

答案 1 :(得分:0)

找到它:正确的意图是ACTION_VIEW

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <!--<category android:name="android.intent.category.OPENABLE"/>-->
        <data android:mimeType="text/*" />
    </intent-filter>
相关问题