gpt4 book ai didi

java - 如何将字符串转换为文件以便在 Android 中共享?

转载 作者:行者123 更新时间:2023-12-02 09:49:15 24 4
gpt4 key购买 nike

我有一个 Android 应用程序,它使用

final JSONArray jsonArray1 = jsonArray;
JSONObject export = jsonArray1.getJSONObject(index);

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

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 对象作为文件最好是 JSON 文件共享,例如附加到电子邮件中。

提前致谢

最佳答案

将以下权限添加到您的 list 中

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

res/xml下创建provider-paths.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();

Intent 在我的案例中打开并在我的应用程序中共享,例如Whatsapp 和电报。稍微解释一下,Android 不再允许将 file:// 附加到 Intents,check this 。因此,您需要使用文件提供程序来实现此目的,如其 website 中所述。 .

关于java - 如何将字符串转换为文件以便在 Android 中共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56435057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com