如何将字符串转换为文件,以便可以在Android中共享它?

时间:2019-06-03 22:00:37

标签: java android

我有一个使用

的android应用
final JSONArray jsonArray1 = jsonArray;
JSONObject export = jsonArray1.getJSONObject(index);

获取JSON对象,然后使用共享意图:

Intent ShareIntent = new Intent(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_SUBJECT, "THE SUBJECT");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "THE CONTENT IS " + export);
startActivity(Intent.createChooser(ShareIntent, "Send the object via"));

并且工作正常,但是,它以纯文本形式共享JSON对象。

我知道为什么会这么做。

如何更改它,以便将JSON对象作为文件preferably a JSON file共享,例如将其附加到电子邮件。

预先感谢

1 个答案:

答案 0 :(得分:1)

将以下权限添加到清单

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

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

        .....
    </application>

provider-paths.xml下创建res/xml文件,并将以下代码放入其中:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_file_path" path="." />
</paths>

您可以创建一个文件,然后将jsonobject写入该文件。

        StringBuilder stringBuilder = new StringBuilder();

        File tempFolder = new File(MainActivity.this.getFilesDir(), "temp");
        if (!tempFolder.exists()) {
            tempFolder.mkdir();
        }

        JSONObject export = new JSONObject();
        try {
            export.put("name", "my-name");
            export.put("age", "20");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        File file = null;
        try {
            Writer wrt = null;
            file = new File(tempFolder, "myJsonFile.json");
            wrt = new BufferedWriter(new FileWriter(file));
            wrt.write(export.toString());
            wrt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("file/text/plain");

        Uri fileUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",
                file);

        shareIntent.putExtra(Intent.EXTRA_TEXT, "THE CONTENT IS " + export.toString());
        shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, "Send the object via"));

        try {

            File f = new File(MainActivity.this.getFilesDir() + "/temp/myJsonFile.json");
            InputStream inputStream = new DataInputStream(new FileInputStream(f));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String str;
            str = bufferedReader.readLine();
            stringBuilder.append(str);

            tvAccountName.setText(stringBuilder.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }

    //then delete the file or you can delete the tempFolder instead;
    if(file!=null)file.delete();

意图在我的情况下是开放的,并且正在我的应用程序中共享,例如Whatsapp和电报。稍微说明一下,Android不再允许将file://附加到Intents check this上。因此,您需要使用文件提供程序来实现此目标,如其website中所述。