简体   繁体   中英

How to send saved CSV file via email or upload with Google Drive in Android?

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.

Here is how I save the data:

StringBuilder data = new StringBuilder();
data.append("Timestamp,Mass,Change in Mass\n");
for(int i = 0; i < 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();

This just combined my ArrayLists into a string and saves the data into csv file with name scale.

Here is how I am attempting to share it:

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..."));

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?

I would appreciate any assistance and am ready to provide clarification upon request.

EDIT Using feedback

I tried one of the proposed solutions. This is what my code looks like now:

    StringBuilder data = new StringBuilder();
    data.append("Timestamp,Mass,Change in Mass\n");
    for(int i = 0; i < 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");
    }

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".

Create an xml file in res/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
     name is the file name
     path is the root of external storage, it means here: Environment.getExternalStorageDirectory()
     -->
    <external-path name="scale" path="."/>

    <!--
    another example:  Environment.getExternalStorageDirectory() + File.separator + "temps" + "myFile.pdf"
     -->
    <external-path name="myFile" path="temps"/>

</paths>

add provider in your application tag in manifest

<!--android:name="android.support.v4.content.FileProvider"-->
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="your.application.package.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

Finally change your code to this:

public static void sendEmailWithAttachment(Context context) {
    String filename="/scale.csv";
    File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
    //Uri path = Uri.fromFile(filelocation);
    Uri path = FileProvider.getUriForFile(context, "your.application.package.fileprovider", filelocation);
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    // set the type to 'email'
    emailIntent .setType("vnd.android.cursor.dir/email");
    String to[] = {"email@gmail.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);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

Some tips about defining file path from android docs


<files-path name="name" path="path" />

Represents Context.getFilesDir()


<cache-path name="name" path="path" />

Represents getCacheDir()


<external-path name="name" path="path" />

Represents Environment.getExternalStorageDirectory().


<external-cache-path name="name" path="path" />

Represents Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)


<external-media-path name="name" path="path" />

Represents Context.getExternalCacheDir().


Read more from docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM