Attempting to send the created text file as an email attachment - from the default folder

I am trying to do something as simple as creating a text file and then send it as an attachment. Although it works fine if I use sdcard, I don’t know where to put it in the “standard data folder”, so my application actually works for everyone without an SD card (and the file is somewhat invisible).

While this code is working, I put questions in the comment <- * .

When creating a file:

String FILENAME = "myFile.txt"; String string = "just something"; // create a File object for the parent directory File myDirectory = new File("/sdcard/myDir/"); // ******** <- what do I have to put HERE for standard data folder???? // have the object build the directory structure, if needed. myDirectory.mkdirs(); // create a File object for the output file File outputFile = new File(myDirectory, FILENAME); // now attach the OutputStream to the file object, instead of a String representation FileOutputStream fos = null; //always have to put try/catch around the code - why? I don't know try { fos = new FileOutputStream(outputFile); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } //again have to put try/catch around it - otherwise compiler complains try { fos.write(string.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

When sending a file:

 public void doSendFile() { String fileName = "/sdcard/myDir/myFile.txt"; // ******** <- what do I have to put HERE for standard data folder???? Intent i = new Intent(Intent.ACTION_SEND); try { mainDataManager.logFileHandle.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } i.setType("text/plain"); i.putExtra(Intent.EXTRA_EMAIL, new String[] { " to@someone.com " }); i.putExtra(Intent.EXTRA_SUBJECT, "subject"); i.putExtra(Intent.EXTRA_TEXT, "text"); Log.i(getClass().getSimpleName(), "logFile=" + Uri.parse("file://" + fileName)); i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fileName)); try { startActivity(Intent.createChooser(i, "Send mail...")); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(getBaseContext(), "There are no email clients installed.", Toast.LENGTH_SHORT) .show(); } } 

I found that the file seems to be stored in the file data / data / com.xxx.xxxx / databases / myFile.txt when created. But when I used this in the application, nothing was sent.

Basically I need to know how to store the file in local memory, and then send it from there. Since not everyone can have an SD card with external memory, I guess.

Thanks!

+4
source share
1 answer

This is due to the fact that the SD card is not protected, so you can write there in standard java mode, just like you.

To write to the Android file system, you cannot use this, since each file is protected with the application key so that other applications do not use it.

You use openFileOutput () to learn more about this guide:

http://www.anddev.org/working_with_files-t115.html

0
source

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


All Articles