writing to storage instead of emulated on nexus 7
Asked Answered
M

1

0

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

Mizell answered 12/4, 2014 at 9:55 Comment(0)
N
0

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

Nexus 7 Mounted on Ubuntu

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

Nexus 7 "Internal storage"

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

Nexus 7 "Internal storage" 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.

Neoterism answered 12/4, 2014 at 11:53 Comment(7)
I'm sorry, I don't understand your answer. How do I get the root directory? Regarding the public directory part, it was a mistake, I experimented with the code, trying to learn something about the directories. I use getExtranalStorageDirectory()Mizell
@Yotam: If I plug a Nexus 7 (2013) into my Ubuntu 13.10 machine, "Nexus 7" shows up in my roster of devices. Clicking on it gives me an "Internal Storage" option. That, despite its name, is the directory that you access programmatically via Environment.getExternalStorageDirectory().Neoterism
I would expect so. However, browsing on my device, I see that I have three paths. /storage/sdcard0 (which the path mounted on my machine), /storage/emulated/0 (which is Envrionment.getExternalStorageDirectory() pints to, and /storage/emulated/legacy (which I have no idea about). I guess there is a link between the two (three?) directories that works in one direction, and not in the other.Mizell
@Yotam: Since none of those directories are visible via native Ubuntu and MTP, I assume that you are using DDMS. In that case, none of them are relevant. Your external storage is /mnt/shell/emulated/0, for the primary device user. Android 4.2+ supports multiple accounts per device on tablets, and so the directories that you see at runtime (when your app runs as a specific account) are not what you will see from DDMS (where it runs as a separate Linux user from any of those).Neoterism
Sorry, I wasn't clear. The path I gave above are those found on my device. On my machine, I only see /run/user/1000/gvfs/mtp:host=%5Busb%3A002%2C005%5D/Internal storageMizell
@Yotam: That Internal storage, when viewed through Nautilus (or perhaps other file managers for Ubuntu -- I haven't tried them) maps to /mnt/shell/emulated/0, which is where Environment.getExternalStorageDirectory() points to for apps running as the primary device user.Neoterism
Something is strange here. I don't see the folder on my machine.Mizell

© 2022 - 2024 — McMap. All rights reserved.