在Android设备上找不到保存的CSV或TXT文件

时间:2019-04-11 03:28:35

标签: android android-intent export-to-csv fileoutputstream

我有三个ArrayLists,我想保存到CSV文件,然后通过我正在构建的应用程序检索并通过电子邮件发送给他们。列表中的两个包含双精度值,最后一个包含时间戳。

这是我尝试将它们保存到CSV文件(在YouTube上看到)的方式:

StringBuilder data = new StringBuilder();
data.append("Timestamp,Mass,Change in Mass");
for(int i = 0; i < mass_list.size(); i++){
     data.append("\n"+String.valueOf(timestamp_list.get(i))+ ","+String.valueOf(mass_list.get(i))+","+String.valueOf(mass_roc_list.get(i)));
}
FileOutputStream out = openFileOutput("scale.csv", Context.MODE_APPEND);
out.write((data.toString()).getBytes());
out.close();

我基本上在列表中滚动并使用逗号将值附加到文件中以将它们分开。这是我尝试检索的方式:

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[] = {"MY_EMAIL"};
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);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

我基本上是试图将文件附加到电子邮件中。这是AndroidManifest.xml文件的摘要:

<provider
     android:name="android.support.v4.content.FileProvider"
     android:authorities="com.example.scaleapp.fileprovider"
     android:grantUriPermissions="true"
     android:exported="false">
     <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/provider_paths" />
 </provider>

还有provider_paths.xml

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

代码运行并且不会崩溃。但是,当我尝试通过电子邮件发送数据时,它说“无法附加空文件”。我对为什么文件为空感到困惑。

问题可能是我错误地另存为CSV。但是,我尝试将扩展名更改为.txt,并且发生了同样的事情。我不确定它的文件保存或文件检索代码是否起作用。感谢您的帮助,如有需要,我们将为您提供澄清。

0 个答案:

没有答案