【问题标题】:Saving files on external storage on Nexus 7 and retrieving from PC在 Nexus 7 上的外部存储上保存文件并从 PC 检索
【发布时间】:2012-12-30 01:10:24
【问题描述】:

对于我的项目,我希望能够将传感器数据保存到文件中,并能够从 PC 访问它以进行进一步分析。这是我到目前为止的代码,它可以成功运行:

File root = Environment.getExternalStorageDirectory();
File csvfile = new File(root, "Sensor_Data.csv");
FileWriter writer = new FileWriter(csvfile, true);
writer.append("Time");
writer.append(',');
writer.append("Position");
writer.append('\n');
Number data[] = new Number[4000];
for (int i = 0; i<4000; i+=2){
    posHistory.toArray(data);
        writer.append(String.valueOf(data[i]));
    writer.append(',');
        writer.append(String.valueOf(data[i+1]));
    writer.append('\n');
}
writer.flush();
writer.close();

问题在于 Nexus 7 实际上没有外部存储(我相信它使用某种模拟器以便与大多数在其代码中使用外部存储的应用程序兼容。)

运行此代码后,使用设备上的 Astro 文件管理器,我在四个位置看到“Sensor_Data.csv”文件:/sdcard、/storage/sdcard0、/storage/emulated/0 和 /storage/emulated/遗产。我可以使用 texteditor 或 documentviewer 从设备中打开它们中的每一个,所有数据都在那里。

问题是,当我将 Nexus 7 连接到 PC 时,我只看到“内部存储”,打开时实际上只显示虚拟 SD 卡的内容,而不是实际的内部存储,但 .csv 文件不是那里。还有一个附加目录“storage/emulated/legacy”,但其中也没有,也没有“sdcard0”或“0”文件夹。当我记录“Environment.getExternalStorageDirectory().getAbsolutePath()”以查看路径是什么时,我得到 /storage/emulated/0

我已尝试将文件保存在内部存储的其他位置但没有成功,但我宁愿将其保存在“外部存储”中,以便该应用与大多数使用外部存储的设备兼容。

更新 所以使用 adb pull filename 后的结果如下:

adb pull /storage/emulated/0/Sensor_Data.csv
remote object '/storage/emulated/0/Sensor_Data.csv' does not exist

adb pull /storage/emulated/legacy/Sensor_Data.csv
243 Kb/s <1495 bytes in 0.006s>

adb pull /storage/sdcard0/Sensor_Data.csv
243 Kb/s <1495 bytes in 0.006s>

adb pull /sdcard/Sensor_Data.csv
243 Kb/s <1495 bytes in 0.006s>

因此,唯一不起作用的位置是 /storage/emulated/0/Sensor_Data.csv,即使 Astro 在所有四个中都看到它并且 LogCat 显示它正在保存到 /storage/emulated/0/

也只是尝试保存到

root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

我再次可以在 Astro 中相同的四个位置看到它,但是当我通过 Windows 资源管理器并导航到下载文件夹时,它不存在。

【问题讨论】:

  • 我找到了灵魂。这是 Nexus 4 和 7 的错误。stackoverflow.com/questions/13737261/…
  • 太棒了,很高兴知道这只是 nexus 7 的错误,而不是应用程序,这意味着它应该可以在任何其他使用实际 SD 卡的 Android 设备上正常工作。我将很快在 nexus 7/4 以外的其他地方测试我的应用程序以确认。希望在 Android 更新中修复此错误只是时间问题。

标签: android file-io save android-sdcard nexus-7


【解决方案1】:

您是否尝试过使用带有“adb pull filename”和 Astro 看到的完整路径名的终端?这应该可以工作,如果你自己使用它不会太难。

【讨论】:

  • 用 adb 拉取结果更新了原始帖子。我知道文件的创建没有任何问题,问题是将文件放到计算机上。当然我可以使用 adb 工具,但是这个应用程序是为没有 adb 工具的用户设计的
【解决方案2】:

我也经历过这种情况(在我的 Nexus 4 上)。该文件存在于我的手机上(使用 adb shell 找到它),但我笔记本电脑上的文件浏览器找不到该文件。就像@billtian 所说,这似乎是 Nexus 4 和 7 设备上的错误。我在旧的 Ericsson Xperia 上试用了我的代码,我可以通过我的笔记本电脑找到该文件。

【讨论】:

    【解决方案3】:

    为什么,最好不要将其发送到您的邮箱。请参阅以下示例:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
    intent.putExtra(Intent.EXTRA_TEXT, "body text");
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, xmlFilename);
    if (!file.exists() || !file.canRead()) {
        Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    Uri uri = Uri.parse("file://" + file.getAbsolutePath());
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Send email..."));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2016-08-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多