-2

I am making an app that makes a file and then I want to email it to myself. The Program just keeps saying: "couldn't attach file" after I choose the email manager. I think I may have the wrong file directory written, but I don't know what the correct one is if it isn't what I already have.

Here is the Email button code

    btnEmailForm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String filelocation="data/data/CyberEye/files/form.txt";  // I think the problem is here

            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            // set the type to 'email'
            emailIntent .setType("vnd.android.cursor.dir/email");
            String to[] = {"myemail@place.edu"};
            emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
            // the attachment
            emailIntent .putExtra(Intent.EXTRA_STREAM,  filelocation);
            // the mail subject
            emailIntent .putExtra(Intent.EXTRA_SUBJECT, "AppName");
            startActivity(Intent.createChooser(emailIntent, "Send email..."));
        }
    });

Here is the Code for my file saving

 btnSaveForm.setOnClickListener(new View.OnClickListener() {
                             @Override
                             public void onClick(View v) {
                                      // TODO Auto-generated method stub
                                      textEventName[0] = editEventName.getText().toString();
                                 try {
                                               FileOutputStream fou = openFileOutput("form.txt", MODE_WORLD_READABLE);
                                               OutputStreamWriter osw = new OutputStreamWriter(fou);
                                               try {
                                                        osw.write(String.valueOf(textEventName));
                                                        osw.flush();
                                                        osw.close();
                                                        Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();

                                                   } catch (IOException e) {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                   }
                                          } catch (FileNotFoundException e) {
                                               // TODO Auto-generated catch block
                                               e.printStackTrace();
                                          }
                                 }
                        });
Rorschach
  • 3,684
  • 7
  • 33
  • 77
  • You must either place the file on the (shared) external storage, make an internal storage file readable by other apps, or use a content provider. Even if you make a normally private internal storage file world readable, email clients may *assume* they can't read it without actually trying, so that approach is not encouraged. – Chris Stratton Nov 12 '14 at 02:07
  • Does anyone know why this got downvoted? I looked through all the instructions. What am I missing? – Rorschach Nov 12 '14 at 03:44
  • What are some names of content providers or forms of shared external storage that I can look into @ChrisStratton ? – Rorschach Nov 12 '14 at 03:55

2 Answers2

0
    btnSendForm.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             writeToSDFile();
                             }
                    });
@Override
protected void onCreate(Bundle savedInstanceState) {

private void writeToSDFile(){

    // Find the root of the external storage.
    // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

    File root = android.os.Environment.getExternalStorageDirectory();
    //tv.append("\nExternal file system root: "+root);

    // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File (root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        String stringeventname = editEventName.getText().toString();


        pw.println("Event Name: "+stringeventname);
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
    //tv.append("\n\nFile written to "+file);
}

}

    btnEmailForm.setOnClickListener(new View.OnClickListener() {
        @Override
                     public void onClick(View view) {

            final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
            ei.setType("plain/text");
            ei.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"myemail.edu"});
            ei.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testing");
            ei.putExtra(android.content.Intent.EXTRA_TEXT, "Here is another test email");

            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            f.setReadable(true, false);                     // This allows external program access
            Uri U = Uri.fromFile(f);
            ArrayList<Uri> uris = new ArrayList<Uri>();
            uris.add(U);

            File fi = new File(Environment.getExternalStorageDirectory().toString());
            for (File tempe : fi.listFiles()) {
                if (tempe.getName().equals("myData.txt")) {
                    fi = tempe;
                    break;
                }
            }


            Uri Us = Uri.fromFile(fi);
            uris.add(Us);



            ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

This ended up working for me in the end. Note the code for the gathering the picture is not included so if you want to use this code you will have to make the temp.jpg file as well.

This code sends the contents of an edit text in one file and a picture in another file and then sends this info to an email address (myemail.edu).

Rorschach
  • 3,684
  • 7
  • 33
  • 77
-1

Don't try to use absolute paths just as strings figure out where your cache dir is

Add a File field to your class File cacheDir

cacheDir = new File(mContext.getCacheDir() + File.separator + "files");

File txtFile = new File(cacheDir + File.separator + "form.txt");

when you write the file do this

FileOutputStream fos = new FileOutputStream(txtFile)

attaching the intent extra

emailIntent.putExtra(Intent.EXTRA_STREAM,txtFile.getAbsolutePath()); //not sure about that part
Brian
  • 4,328
  • 13
  • 58
  • 103