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('\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();
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.
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.
I experienced this as well (on my Nexus 4). The file existed on my phone (found it using adb shell), but my file explorer on my laptop couldn't find the file. Like @billtian said, this seems to be a bug on Nexus 4 and 7 devices. I tried out my code on an old Ericsson Xperia and there I could locate the file via my laptop.
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..."));
来源:https://stackoverflow.com/questions/14346160/saving-files-on-external-storage-on-nexus-7-and-retrieving-from-pc