Attach PDF to Email from Android Application - Zero File Size

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.

+6
source share
4 answers
 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:")); 
+6
source

If the download.pdf file exists in the SDCard. So the problem should be getting the Uri from File. Try this, it works for me.

 Uri uri = Uri.fromFile(new File("/sdcard/", "download.pdf")); 
0
source

The same problem arises for me. I cleared this using some method from the examples. I have already answered a question similar to your request. Perhaps this can help you.

0
source
 File externalStorage = Environment.getExternalStorageDirectory(); String PDFpath = externalStorage.toString(); String pdfpath =path.replace("/mnt",""); Uri uri = Uri.parse(new File("file://" + pdfpath)); 
0
source

Source: https://habr.com/ru/post/916255/


All Articles