I am trying to attach a PDF file called download.pdf to an email in my Android application. First, I copy the file to SDCard and attach it to the email.
I'm not very appropriate, but I'm testing a galaxy 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 attached file is 0 bytes in size. Can anyone determine what might be wrong?
EDIT
I see that the file was saved on the device if I look in the settings, so this is a problem related to the way I attach the file to the message. In the error log, I see:
gMail Attachment URI: file:///mnt/sdcard/download.pdf gMail type: application/pdf gmail name: download.pdf gmail size: 0
EDIT
I wonder if this is a mistake on the galaxy tab? If I open the file through the pdf viewer (from my application), then try connecting to gmail email, the size will be 0. Can anyone check?
Thanks.
source share