Android - 无法在电子邮件中附加文件 [英] Android - not able to attach a file in email

查看:124
本文介绍了Android - 无法在电子邮件中附加文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,保存到内部存储的文件对您的应用程序是私有的,其他应用程序无法访问它们(用户也不可以使用)。



我可以看到在DDMS中的文件/ data / data / package_name / files / in file explore中,但是当我在电子邮件中使用imageUri附加了上述文件URI时,我看到附件文件是0kb
i使用了默认的电子邮件Android的API。



任何人都可以建议我,如何在电子邮件中附加一个文件,这是私人的应用程序?



尽管我能够将文件保存在SD卡中并从SD卡中附加文件,但这是正常的。



但是如果SD卡不可用并将文件保存到内部存储器中,然后如何将它们附加到电子邮件中。

  String FILENAME =hello_file.txt ; 
String string =hello world!; FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.cl OSE();

文件imageFile = getFileStreamPath(FILENAME);
Uri imageUri = Uri.fromFile(imageFile);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType(* / *);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,imageUri);

this.startActivityForResult(Intent.createChooser(emailIntent,Send mail ...),SUB_ACTIVITY);


解决方案

当您尝试从内部存储中附加文件时,在日志中写入错误:

  ERROR / Gmail(...):file://附件路径必须指向文件:/// MNT / SD卡。 

电子邮件应用程序会显示附件文件,即使它没有实际存在。 p>

对于外部存储,文档说:


每个Android兼容设备支持可以用来保存文件的共享外部存储。这可以是可移动存储介质(如SD卡)或内部(不可移动)存储。


这意味着不必担心设备根本没有外部存储。不过,外部存储可能无法使用。请参阅 http://developer.android.com/guide/topics /data/data-storage.html#filesExternal


By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

I am able to see the file "/data/data/package_name/files/ in file explore in DDMS, but when i attached the above file URI using imageUri in email , then i saw that attached file is of 0kb. i have used the default email APIs of Android.

Can anyone suggest me ,how to attach a file in email that is private to the application?

although i am successful able to save the file in SD card and attaching the file from SD card , this is working fine.

But if SD card is not available and saving the file to the internal storage , then how can i attach them in email.

String FILENAME = "hello_file.txt";
String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME,     Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

File imageFile = getFileStreamPath(FILENAME );
Uri imageUri = Uri.fromFile(imageFile);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,imageUri);

this.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."),SUB_ACTIVITY);

解决方案

When you try to attach file from internal storage, GMail writes an error to the log:

ERROR/Gmail(...): file:// attachment paths must point to file:///mnt/sdcard. 

E-mail application would show you the attached file even if it didn't physically exist.

As for an external storage, documentation says that:

Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

That means you don't have to worry about device not having an external storage at all. Still, external storage can be unavailable at times. Refer to http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

这篇关于Android - 无法在电子邮件中附加文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆