问题附加文本文件以电子邮件Intent对象

问题描述:

我有我需要用我的应用程序生成的文本文件附加到电子邮件作曲家的要求。

I have a requirement that I need to attach text files generated by my app to the email composer.

我已经创建了我的档案在我的应用程序专用区(getFilesDirectory())。

I have created my files at my applications private area (getFilesDirectory()).

我附上该文件路径电子邮件意图对象的URI。我能够看到电子邮件作曲家的附件,但我无法接收的附件。

I am attaching URI of that file path to the email Intent object. I am able to see the attachments in email composer but I am unable to receive the attachments.

我使用这个说法的文件附加到意图对象

I am using this statement attach the files to the intent object

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(context.getFilesDirectory()+"abc.txt"));

在谷歌做一些搜索后我才知道getFilesDirectory()是唯一的私人我的应用程序和其他应用程序的无法访问我的文件(我的情况下,其电子邮箱)。

after doing some search in google I got to know "getFilesDirectory()" is only private to my application and other app's can't access my files (my case its E-mail).

由于许多设备可能没有SD卡的,我不能保存在SD卡我的文件。

As many of devices may not have SD-card ,I can't kept my files in SD-Card.

请让我知道我可以存储我的文件,这样我可以我的文件附加到电子邮件应用程序。
 谢谢。

Please let me know where I can store my files so that I can attach my files to the email application. thanks.

这是不可能的重视从应用领域的文件,你需要先复制到SD卡(临时或没有)。如果在系统中没有SD卡,应该通知用户。我一直在这个最近处理了,我没有发现任何替代解决方案。

It is not possible to attach a file from the application area, you need to copy first into the SDCard (temporary or not). If there is no SDCard in the system, you should notify the user. I have been dealing recently with this, and I didn't find any alternative solution.

您可以使用以下code复制文件到SD卡:

You can copy the file to the SDCard using the following code:

File data = Environment.getDataDirectory();
if (sd.canWrite()) {
    String currentDBPath = "\\data\\application.package\\databases\\name";
    String backupDBPath = "name";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    if (currentDB.exists()) {
        FileChannel src;
        try {
            src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            try {
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            } catch (IOException e) {                                                    
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

记住要添加以下权限:机器人:名字=android.permission.WRITE_EXTERNAL_STORAG​​E

Remember to add the following permission: android:name="android.permission.WRITE_EXTERNAL_STORAGE"