English 中文(简体)
Android: 如何发送电子邮件意向, 使用带有特殊字符的文件?
原标题:Android: how to send email intent, using file with special characters?
  • 时间:2012-05-25 19:37:55
  •  标签:
  • android

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

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"));

然而,通过 gmail 发送的电子邮件如果有 < code> fileName=\" test#. txt" , 则不包含附件 。

我尝试用 URLEncoder 编码路径, 如下文所示, 但是这与“ text. txt ” 或“ text#. txt ” 无效 。

我可能漏掉了一些简单的东西, 但我应该如何将文件路径编码为特殊字符, 用于发送意向书?

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"));
最佳回答

s 因为您没有正确编码 URL 。 您没有只编码文件名, 而是编码了完整的 URL, 结果 :

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

试试这个代替:

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

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签