问题
In my code, write stuff to file using this code
//the path
String fileName = Environment.getExternalStoragePublicDirectory(fileName) + File.separator+"Foo"+File.separator;
File dir = new File(fileName);
//the file
fileName += "bar"
File file = new File(fileName);
try {
file.createNewFile();
} catch (IOException e) {
//do nothing, for now
}
return;
This results with the file being written /storage/emulated/0/
and not to /storage/sdcard0
. The problem I have with this is that I dont see /storage/emulated/0/
when I connect the device to my machine (Ubuntu 13.10).
回答1:
When you "connect the device to [your] machine", you do not get the root directory of the device. You will have access to external storage.
Also, getExternalStoragePublicDirectory() does not take random values as parameters. Use something like Environment.DIRECTORY_DOWNLOADS
.
For example, here is what I see on Ubuntu 13.10 when I plug in a Nexus 7 (2013):

When I go into "Internal storage", I see:

Here is the contents of /mnt/shell/emulated/0/
, as viewed in DDMS:

You will notice that the directory listing for "Internal storage" is the same as it is for /mnt/shell/emulated/0/
. That is because they are the same directory.
For the primary user account, Environment.getExternalStorageDirectory()
points to a spot that, when viewed in DDMS, shows up as /mnt/shell/emulated/0/
and, via MTP, shows up as "Internal storage" (though the latter is the Nexus 7's name for it -- it may vary by device).
Also note that MTP may not show everything that is in /mnt/shell/emulated/0/
, because MTP is really driven by MediaStore
, and not everything may have been indexed yet. If you write files to external storage, use MediaScannerConnection
and scanFile()
to ensure that the device's end of the MTP connection knows about them.
And I apologize for the mixed "internal storage" / "external storage" terminology. I just got through writing a blog post series trying to explain all of that.
来源:https://stackoverflow.com/questions/23028824/writing-to-storage-instead-of-emulated-on-nexus-7