Attaching a PDF to an Email from Android App - File Size is Zero

GuybrushThreepwood picture GuybrushThreepwood · May 21, 2012 · Viewed 8k times · Source

I am trying to attach a PDF file called download.pdf to an email in my Android App. I am copying the file first to the SDCard and then attaching it the email.

I'm not if relevant, but I am testing on a galaxy tab device. The external storage path returns mnt/sdcard/

My code is as follows :

public void sendemail() throws IOException {

    CopyAssets();

    String emailAddress[] = {""};

    File externalStorage = Environment.getExternalStorageDirectory();

    Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "download.pdf"));

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
    emailIntent.setType("application/pdf");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(emailIntent, "Send email using:"));

    }

public void CopyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        if (filename.equals("download.pdf")) {
        try {
          System.out.println("Filename is " + filename);
          in = assetManager.open(filename);
          File externalStorage = Environment.getExternalStorageDirectory();
          out = new FileOutputStream(externalStorage.getAbsolutePath() + "/" + filename);
          System.out.println("Loacation is" + out);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }       
    }
    }
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
}

The problem is that the file that is attached is 0 bytes in size. Can anyone spot what might be wrong ?

EDIT

I can see that the file has been saved onto the device if I look in settings, therefore this must be a problem around how I am attaching the file to the email. In the error log I am seeing :

gMail Attachment URI: file:///mnt/sdcard/download.pdf
gMail type: application/pdf
gmail name: download.pdf
gmail size: 0

EDIT

Wondering if this is a bug on the galaxy tab ? If I open the file via a pdf viewer (from my app) then try to attach to a gmail email, the size is again 0. Can anyone verify ?

Thank you.

Answer

user5068137 picture user5068137 · Jul 9, 2015
String[] mailto = {""};
                        Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/REPORTS/",pdfname ));
                        Intent emailIntent = new Intent(Intent.ACTION_SEND);
                        emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
                        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Calc PDF Report");
                        emailIntent.putExtra(Intent.EXTRA_TEXT, ViewAllAccountFragment.selectac+" PDF Report");
                        emailIntent.setType("application/pdf");
                        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                        startActivity(Intent.createChooser(emailIntent, "Send email using:"));