如何通过电子邮件发送保存的 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 上传? 的相关文章

  • Android 中的列表视图分页

    我有一个列表视图 其中显示了 50 个元素 我决定对视图进行分页 以便视图的每个部分都有 10 个元素 然后单击 下一个 按钮以获取下一个 10 个元素 如何设置10个数据 我关注这篇文章http rakhi577 wordpress co
  • Java-如何将黑白图像加载到二进制中?

    我在 FSE 模式下使用 Java 和 swing 我想将完全黑白图像加载为二进制格式 最好是二维数组 并将其用于基于掩码的每像素碰撞检测 我什至不知道从哪里开始 过去一个小时我一直在研究 但没有找到任何相关的东西 只需将其读入Buffer
  • android textview 有字符限制吗?

    我正在尝试在 android TextView 中输入超过 2000 3000 个字符 它不显示任何内容 任何一份指南是否对 android textview 有字符限制或什么 我在G3中做了一些小测试 我发现 如果activtiy布局中有
  • 即使 Android M 上的移动数据已打开(有连接),也可以通过 WiFi(无连接)发送请求

    我必须在没有互联网连接的情况下将 UDP 数据包发送到 WiFi 模块 配有自己的 AP 但是当我将手机连接到 AP 时 Android 会在移动数据接口上重定向我的数据包 因为它有互联网连接 我使用下面的代码来完成我的工作 但它似乎不适用
  • 发生错误。请参阅日志文件 - eclipse juno

    每当我启动 Eclipse Juno 时 都会出现错误 发生错误 请查看日志文件 C Program Files eclipse configuration 1362989254411 log 有的网站说卸载jdk重新安装 我这样做了 但没
  • 当您在数组列表上调用remove(object o)时,它如何比较对象?

    当您在 java 中的数组列表上调用remove object o 时 它如何比较对象以找到要删除的正确对象 它使用指针吗 或者它使用 Comparable 接口来比较对象吗 ArrayList remove 依赖于对象的实现Equal方法
  • 在 Java 中将弯音发送到 MIDI 音序器

    我了解启动和运行 MIDI 音序器的基础知识 并且希望能够在播放过程中增加 减小序列的音高 但弯音是发送到合成器而不是音序器的消息 我尝试将音序器的接收器设置为合成器的发射器 当我发送弯音短消息时 音序器保持相同的音调 但随后合成器以新的弯
  • SDK >=26 仍需要 mipmap/ic_launcher.png?

    在 Android 中 有两种指定启动器图标 可以说是应用程序图标 的方法 老 方式 在 mipmap 文件夹中指定不同的 png 文件 通常命名为 ic launcher png 但可以通过以下方式设置名称android icon mip
  • Glass 语音命令给定列表中最接近的匹配项

    使用 Glass 您可以通过 确定 Glass 菜单启动应用程序 它似乎会选择最接近的匹配项 除非命令相距数英里 并且您可以明显看到命令列表 无论如何 是否可以从应用程序内或从语音提示 在初始应用程序触发后 给出类似的列表并返回最接近的匹配
  • 如何构建自定义摄像机应用程序?

    我正在尝试开发一个自定义摄像机录像机 当我的设备在 Activity 的 beginRecording 中执行 start MediaRecorder 方法时 应用程序崩溃 我不知道出了什么问题 因为我遵循谷歌API指南 http deve
  • 如何在 Android 上将动态 alpha 遮罩应用于文本

    I want to make a dynamic alpha mask with drawable shapes as circles or whatever and apply it to a drawed text on Android
  • 由于“进程崩溃”,仪器运行失败。

    我想运行以下测试 package com xxx yyy import android content Context import androidx test InstrumentationRegistry import androidx
  • JavaFX - 为什么多次将节点添加到窗格或不同的窗格会导致错误?

    我现在正在学习基本的 JavaFX 我不明白我正在阅读的书中的这一说法 不 诸如文本字段之类的节点只能添加到一个窗格中一次 将节点添加到多次窗格或不同的窗格将导致运行时错误 我可以从书中提供的UML图看出它是一个组合 但我不明白为什么 库类
  • 在android中创建SQLite数据库

    我想在我的应用程序中创建一个 SQLite 数据库 其中包含三个表 我将向表中添加数据并稍后使用它们 但我喜欢保留数据库 就好像第一次安装应用程序时它会检查数据库是否存在 如果存在则更新它 否则如果不存在则创建一个新数据库 此外 我正在制作
  • 如何初始化静态地图?

    你会如何初始化静态Map在Java中 方法一 静态初始化方法二 实例初始化 匿名子类 或者 还有其他方法吗 各自的优点和缺点是什么 这是说明这两种方法的示例 import java util HashMap import java util
  • Android AdMob:addView 在返回活动之前不会显示广告

    我正在尝试在游戏顶部添加横幅广告 我的活动使用带有自定义 SurfaceView 的relativelayout 我希望广告与 SurfaceView 重叠 广告会加载并可点击 但不会绘制到屏幕上 当我离开活动并返回时 会绘制广告 例如 通
  • Flash 对象未显示在phonegap android 中

    我已经在 android 手机间隙创建了一个应用程序 我有一个屏幕 我想显示一个静态 flash obj 所以我在屏幕 HTML 页面中放入了以下代码
  • Java:使用 Graph API 在线更新 Sharepoint 上的 docx 文件

    我在使用 Java 在线更新 Sharepoint 上的 docx 文件时遇到问题 首先 我检查了构建 PUT 请求的 URL 此处 并使用此请求 PUT drives drive id items item id content 我首先使
  • R.java是手动修改的!恢复到生成的版本

    我在布局中添加了一个 xml 文件 之后这个错误就来了 但问题是我还没有接触过 R java 文件 现在 在我的新活动中 我要将其内容视图设置为我新创建的 xml 文件 但是当我执行 R layout 时 新创建的 xml 不会出现在建议中
  • Java、Spring、Hibernate找不到org.springframework.orm.hibernate3.LocalSessionFactoryBean

    我正在尝试制作 spring hibernate ant 项目 目前我收到此错误 HTTP Status 500 type Exception report message description The server encountere

随机推荐