Skip to content Skip to sidebar Skip to footer

Problem Attaching Text Files To Email Intent Object

I have a requirement that I need to attach text files generated by my app to the email composer. I have created my files at my applications private area (getFilesDirectory()). I am

Solution 1:

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.

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

Filedata= Environment.getDataDirectory();
if (sd.canWrite()) {
    StringcurrentDBPath="\\data\\application.package\\databases\\name";
    StringbackupDBPath="name";
    FilecurrentDB=newFile(data, currentDBPath);
    FilebackupDB=newFile(sd, backupDBPath);
    if (currentDB.exists()) {
        FileChannel src;
        try {
            src = newFileInputStream(currentDB).getChannel();
            FileChanneldst=newFileOutputStream(backupDB).getChannel();
            try {
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            } catch (IOException e) {                                                    
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

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

Post a Comment for "Problem Attaching Text Files To Email Intent Object"