Note that there are some explanatory texts on larger screens.

plurals
  1. POAttaching a PDF to an Email from Android App - File Size is Zero
    text
    copied!<p>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.</p> <p>I'm not if relevant, but I am testing on a galaxy tab device. The external storage path returns mnt/sdcard/</p> <p>My code is as follows :</p> <pre><code>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); } } } </code></pre> <p>The problem is that the file that is attached is 0 bytes in size. Can anyone spot what might be wrong ?</p> <p><strong>EDIT</strong></p> <p>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 :</p> <pre><code>gMail Attachment URI: file:///mnt/sdcard/download.pdf gMail type: application/pdf gmail name: download.pdf gmail size: 0 </code></pre> <p><strong>EDIT</strong></p> <p>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 ?</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload