Android send xml file does not send attachment

I have seen some examples, but still do not understand why, when I edit the mail, I see the attached .xml, but when I receive without attachments!

Here is my code

File f = new File("data/data/xxx/files/xxx.xml"); Boolean b1 = f.exists(); Boolean b2 = f.canRead(); if (b1 && b2) { Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_EMAIL, ""); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + f.getAbsolutePath())); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "XXX"); sendIntent.putExtra(Intent.EXTRA_TEXT, R.string.mail_body); startActivity(Intent.createChooser(sendIntent, "Email:")); } else { ... 

Ah, only the detail ... when I select the application to send, there is no object or body, even if I wrote putExtra (Intent.EXTRA_SUBJECT) and putExtra (Intent.EXTRA_TEXT), but this is detailed information ...

Edit: I was just debugging my intention: it says “NOT CACHED” in the stream value, how to solve it?

enter image description here

0
source share
2 answers

You cannot attach a file from the internal storage directly for any security purpose, so first you need to copy this file from the internal to the external directory, and then send it by mail if you want to delete this file from the external storage in onActivityResult ().

Here is the code:

 private File copyFileToExternal(String fileName) { File file = null; String newPath = Environment.getExternalStorageState()+"/folderName/"; try { File f = new File(newPath); f.mkdirs(); FileInputStream fin = openFileInput(fileName); FileOutputStream fos = new FileOutputStream(newPath + fileName); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = fin.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fin.close(); fos.close(); file = new File(newPath + fileName); if (file.exists()) return file; } catch (Exception e) { } return null; } 

Email sending method:

 private void sendEmail(String email) { File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml"); Uri path = Uri.fromFile(file); Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setType("application/octet-stream"); intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); String to[] = { email }; intent.putExtra(Intent.EXTRA_EMAIL, to); intent.putExtra(Intent.EXTRA_TEXT, message); intent.putExtra(Intent.EXTRA_STREAM, path); startActivityForResult(Intent.createChooser(intent, "Send mail..."), 1222); } 

and then

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1222) { File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml"); file.delete(); } } 

Call this method as follows:

  copyFileToExternal(filename + ".xml"); sendEmail(EmailId); 
+4
source

I have seen some examples, but still do not understand why, when I edit the mail, I see the attached .xml, but when I receive without attachments!

Firstly, third-party applications cannot read the internal memory of your application.

Secondly, this may be the wrong path to the internal storage of your application. Never specify hard code paths . For example, your application will not be available for additional accounts and restricted profiles on Android 4.2 tablets. Always use a method, such as getFilesDir() , to get a piece of internal memory.

You will need to either copy the file to external storage, or, even better, use FileProvider to serve your file from internal storage . via content:// Uri .

+1
source

All Articles