如何通过电子邮件发送保存的 CSV 文件或在 Android 中使用 Google Drive 上传?

2024-04-30

我有一个简单的日志记录应用程序,它将数据收集到三个数组列表中,我想将其保存到 CSV 文件中,然后共享到 Google Drive、电子邮件等。

这是我保存数据的方法:

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();

这只是将我的 ArrayLists 组合成一个字符串,并将数据保存到具有名称范围的 csv 文件中。

以下是我尝试分享的方式:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[{"[email protected] /cdn-cgi/l/email-protection"});
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..."));

当我在电子邮件中尝试此操作时,没有附件,只有正文。当我尝试使用 Google Drive 时,只有正文被保存到文本文件中。我不确定我做错了什么,但这可能与文件位置有关。也许我找不到我保存的文件?

我将不胜感激任何帮助,并准备根据要求提供说明。

编辑使用反馈

我尝试了建议的解决方案之一。这就是我的代码现在的样子:

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

一切都编译并正常运行。但是,当我上传到云端硬盘时,它显示“无法上传”,当我发送电子邮件时,它显示“无法附加空文件”。


在中创建一个 xml 文件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>

在您的应用程序标签中添加提供商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>

最后将您的代码更改为:

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 protected] /cdn-cgi/l/email-protection"};
    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..."));
}

关于从 android 文档定义文件路径的一些技巧


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

代表 Context.getFilesDir()


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

代表 getCacheDir()


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

表示Environment.getExternalStorageDirectory()。


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

代表 Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)


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

代表Context.getExternalCacheDir()。


从文档中阅读更多内容 https://developer.android.com/reference/android/support/v4/content/FileProvider

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过电子邮件发送保存的 CSV 文件或在 Android 中使用 Google Drive 上传? 的相关文章

随机推荐