Misha Akopov
Misha Akopov

Reputation: 13057

Android Send Mail with pdf

In My Application I download PDF file to internal storage. after this I want to send mail with the file. I see the file is dowloaded in internal memory com.my.app -> files-> pdffile.pdf and it has permissions -rw-------

when I attach to mail the file and send gmail says: could't send attachment. But why ?? I have permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

this is code for dowloading file. it runs in async task

public static boolean saveFile(String fileName, Context context){
    String fileDirectory = context.getFilesDir().getAbsolutePath()
            + "//"+fileName;
    String urlServ = Constants.serverUrl+ "upload/forms/"+fileName;
    urlServ = urlServ.replace(" ", "%20");
    urlServ = urlServ.replace("\n", "%0d");
    urlServ = urlServ.replace("\"", "%22"); 


    int count;
    URI fUri = URI.create("file://" + fileDirectory);
    File f  = new File(fileDirectory);
    if (f.exists()){
        f.delete();
    }


    try {

        URL url = new URL(urlServ);
        URLConnection conexion = url.openConnection();
        conexion.connect();

        int lenghtOfFile = conexion.getContentLength();

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(fileDirectory);


        //OutputStream output = context.openFileOutput(fileDirectory, Context.MODE_PRIVATE);

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}

And this is code for sending mail :

public static void sendMail(Context context, String filename) {
    String fileDirectory = context.getFilesDir().getAbsolutePath()
            + "/"+filename;
    File f  = new File(fileDirectory);
    Uri URI =Uri.fromFile(f);
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    i.putExtra(Intent.EXTRA_STREAM, URI);

    i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    try {
        context.startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }               
}

What is wrong ? it sands mail only with subject and text... maybe there is some permissions issue. How can I donwload file and give it full permissions

EDIT:

It is permission issue, because when I send file with different permission from the same directory, mail with attachment is being sent. -rw-rw-rw- with this permission

How Can i donwload file and set -rw-rw-rw- permission to it ???

Upvotes: 0

Views: 11062

Answers (2)

Misha Akopov
Misha Akopov

Reputation: 13057

I have solved the problem, if anyone faces the same issue. When opening OutputStream like this it gives new file permission -rw-rw-rw. and other application(Gmail in this case) can use it.

OutputStream output = context.openFileOutput( fileName, Context.MODE_WORLD_READABLE);

Upvotes: 1

HexAndBugs
HexAndBugs

Reputation: 5799

The problem here is that although your app has permissions to read the file, the email application doesn't have permission to read the file. Since Jelly Bean, StrictMode has produced a warning when you try to share a File URI outside your application because this kind of problem can occur where the app you are sharing a file with does not have permission to access the file. It is advised to use a content:// URI when sharing files between apps instead of a file:// URI.

I'd suggest using the FileProvider class, which provides a relatively simple way to share your files using a content:// URI.

Upvotes: 0

Related Questions