开发者

Unable to add file from Internal storage as email attachment

开发者 https://www.devze.com 2023-02-24 10:13 出处:网络
I am creating a file into Internal storage (/data/data/package_name/myfile_name), I want to send that file with attachment but I am getting a blank file in attachment(although I checked that file from

I am creating a file into Internal storage (/data/data/package_name/myfile_name), I want to send that file with attachment but I am getting a blank file in attachment(although I checked that file from file explorer, that file is present and not empty at same location).

And same code runn开发者_如何学Cing well when I used external storage( I am getting my actual file in attachment). Is there any restrictions that we can not send file which are present in internal storage? Or other steps I am missing?


I'm assuming you are trying to send the file as an email attachment using intents.

The reason why the file is empty is that the email app does not have access to the file in /data/data/package_name/myfile_name, due to Androids security model (the /data/data/package_name directory is private to your app).

In order to add the file as an attachment, you need to write it to public storage (such as the SD card) so the email app can access it.


How do you know the file exists at the path you're interested in? Can you view it with DDMS or ADB after your application saved it there? What code are you using to save/read the file? I may be able to provide more specific assistance with that information.

The method used to obtain the internal storage directory on any given device is Context.getFilesDir(). To create a reference to a file named "myfile.dat", for instance:

File myFile = new File(getFilesDir(),"myfile.dat");

Assuming you call the code from inside an Activity or other Context. In order to attach this file to an email, you would be passing a Uri to that location as an extra, so let's add the creation of that to the example:

File myFile = new File(getFilesDir(),"myfile.dat");
Uri fileUri = Uri.fromFile(myFile);

This is all assuming the file was properly saved into Internal Storage in the first place.

Hope that Helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号