title: How to send saved CSV file via email or upload with Google Drive in Android?
body: <p>I have a simple logging app that collects data into three arraylists, which I want saved to a CSV file and then shared to Google Drive, email, etc.</p>
  
  <p>Here is how I save the data:</p>
  
  <pre><code>StringBuilder data = new StringBuilder();
  data.append("Timestamp,Mass,Change in Mass\n");
  for(int i = 0; i &lt; mass_list.size(); i++){
        data.append(String.valueOf(timestamp_list.get(i))+ ","+String.valueOf(mass_list.get(i))+","+String.valueOf(mass_roc_list.get(i))+"\n");
        }
  FileOutputStream out = openFileOutput("scale.csv", Context.MODE_APPEND );
  out.write(data.toString().getBytes());
  out.close();
  </code></pre>
  
  <p>This just combined my ArrayLists into a string and saves the data into csv file with name scale.</p>
  
  <p>Here is how I am attempting to share it:</p>
  
  <pre><code>Intent emailIntent = new Intent(Intent.ACTION_SEND);
  emailIntent.setType("text/plain");
  emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[{"email@gmail.com"});
  emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Scale Data");
  emailIntent.putExtra(Intent.EXTRA_TEXT, "This is the body");
  emailIntent.putExtra(Intent.EXTRA_STREAM, Environment.getExternalStorageDirectory() + "/scale.csv");
  startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  </code></pre>
  
  <p>When I try this in an email, there is no attachment, just the body. When I try with Google Drive, just the body gets saved to a text file. I am not sure what I am doing wrong, but it probably has something to do with file locations. Maybe I am unable to find the file that I saved in? </p>
  
  <p>I would appreciate any assistance and am ready to provide clarification upon request.</p>
  
  <p>EDIT Using feedback</p>
  
  <p>I tried one of the proposed solutions. This is what my code looks like now:</p>
  
  <pre><code>    StringBuilder data = new StringBuilder();
      data.append("Timestamp,Mass,Change in Mass\n");
      for(int i = 0; i &lt; mass_list.size(); i++){
          data.append(String.valueOf(timestamp_list.get(i))+ ","+String.valueOf(mass_list.get(i))+","+String.valueOf(mass_roc_list.get(i))+"\n");
      }
      try {
          //saving data to a file
          FileOutputStream out = openFileOutput("scale.csv", Context.MODE_APPEND);
          out.write(data.toString().getBytes());
          out.close();
  
          Context context = getApplicationContext();
          String filename="/scale.csv";
          File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
          Uri path = FileProvider.getUriForFile(context, "com.example.scaleapp.fileprovider", filelocation);
          Intent emailIntent = new Intent(Intent.ACTION_SEND);
          // set the type to 'email'
          emailIntent.setType("vnd.android.cursor.dir/email");
          String to[] = {"email.com"};
          emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
          emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Scale Data");
          emailIntent.putExtra(Intent.EXTRA_TEXT, "This is the body");
          emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          // the attachment
          emailIntent.putExtra(Intent.EXTRA_STREAM, path);
  
          //this line is where an exception occurs and "Error" is displayed on my phone
          startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  
          infoView.setText("Something worked!");
      }
      catch(Exception e){
          e.printStackTrace();
          infoView.setText("Error");
      }
  </code></pre>
  
  <p>Everything compiles and runs normally. However, when I upload to Drive, it says "unable to upload" and when I send an email, it says "unable to attach an empty file". </p>