在 Nexus 7 上的外部存储上保存文件并从 PC 检索 [英] Saving files on external storage on Nexus 7 and retrieving from PC

查看:39
本文介绍了在 Nexus 7 上的外部存储上保存文件并从 PC 检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For my project I want to be able to save sensor data to a file, and be able to access it from a PC for further analysis. This is the code I have so far and it works successfully:

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('
');
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('
');
}
writer.flush();
writer.close();

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

The problem is that the Nexus 7 doesn't actually have external storage (I believe it uses some sort of emulator in order to be compatible with most apps which make use of external storage in their code.)

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

After running this code, using Astro File manager on the device, I see a "Sensor_Data.csv" file in four locations: /sdcard, /storage/sdcard0, /storage/emulated/0, and /storage/emulated/legacy. And each of them I can open from within the device using texteditor or documentviewer and all the data is there.

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

The problem is that when I connect the Nexus 7 to the PC, I only see "Internal storage" which when opened actually shows me only the virtual SD Card contents, not actual internal storage, but the .csv file isn't there. There is an additional directory "storage/emulated/legacy" but nothing in there either, and no "sdcard0" or "0" folders either. When I Log "Environment.getExternalStorageDirectory().getAbsolutePath()" to see exactly what the path is I get /storage/emulated/0

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

I've tried saving the file elsewhere in internal storage with no success, but I would rather keep it in "external storage" so that the app is compatible with most devices which use external storage.

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

UPDATE So after using adb pull filename here are the results:

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/

So, the only location that doesn't work is /storage/emulated/0/Sensor_Data.csv even though Astro sees it in all four and LogCat shows that it is being saved to /storage/emulated/0/

也只是尝试保存到

root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

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

again I can see it in Astro in the same four locations, but when I go through Windows Explorer and navigate to the downloads folder its not there.

推荐答案

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

Why, best not to send it to your mail. See the following example:

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

这篇关于在 Nexus 7 上的外部存储上保存文件并从 PC 检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆