gpt4 book ai didi

Android:如何使用带有特殊字符的文件发送电子邮件 Intent ?

转载 作者:行者123 更新时间:2023-11-30 04:11:44 24 4
gpt4 key购买 nike

我可以使用以下代码将文本文件附加到电子邮件:

String fileName = "test.txt";
path = "file://" + Environment.getExternalStorageDirectory() + "/" + fileName;

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
startActivity(Intent.createChooser(sendIntent, "Email"));

但是,如果 fileName="test#.txt",通过 gmail 发送的电子邮件不包含附件。

我尝试使用 URLEncoder 对路径进行编码,如下所示,但这不适用于“text.txt”或“text#.txt”。

我可能在这里遗漏了一些简单的东西,但是我应该如何使用特殊字符对文件路径进行编码以发送 Intent ?

String fileName = "test.txt";
// String fileName = "test#.txt";

String path = "file://" + Environment.getExternalStorageDirectory() + "/" + fileName;
String encPath = URLEncoder.encode(path);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(encPath));
startActivity(Intent.createChooser(sendIntent, "Email"));

最佳答案

那是因为您没有正确编码 URL。您不仅对文件名进行了编码,还对完整的 URL 进行了编码,结果是:

file://te#st.txt
file%3A%2F%2Fte%23st.txt

试试这个:

String path = "file://" + Environment.getExternalStorageDirectory() + "/";
path += URLEncoder.encode( fileName );

关于Android:如何使用带有特殊字符的文件发送电子邮件 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10760428/

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