gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:45:42 24 4
gpt4 key购买 nike

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

这是我保存数据的方法:

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

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

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

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

当我在电子邮件中尝试此操作时,没有附件,只有正文。当我尝试使用 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");
}

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

最佳答案

res/xml/provider_paths.xml 中创建 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>

list 的应用程序标记中添加提供商

<!--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@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..."));
}
<小时/>

有关从 Android 文档定义文件路径的一些提示

<小时/>
<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

关于java - 如何通过电子邮件发送保存的 CSV 文件或在 Android 中使用 Google Drive 上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com