Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

android - Saving files on external storage on Nexus 7 and retrieving from 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();

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

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.

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.

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>

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/

Also just tried saving to

root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried using a terminal with "adb pull filename" and the full pathname that Astro sees? That ought to work, and if you're using this yourself wouldn't be too hard.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...