My app provides a file browser. I set the root of the file browser to: Environment.getExternalStorageDirectory(); which, on my Acer Iconia Tab, returns "/mnt/sdcard". However, many 3.0 devices support usb storage. Is there a safe/proper way to get the path to "/mnt"? (to the directory that contains the sdcard and usb storage)
If Environment.getExternalStorageDirectory(); gives /mnt/sdcard/, i was hoping there would be a safe method to get /mnt/usb_storage. There does not appear to be. However, Environment.getExternalStorageDirectory().getParentFile() appears to reliable give the parent directory of /mnt/usb_storage.
If you are talking about devices like the Motorola Xoom that do not have sdcard support then it is safe to use Environment.getExternalStorageDirectory(). The Xoom actually has the sdcard folder structure on the internal storage.
Related
I've already figured out that on some devices the externalStorage may be set as the device Storage, like on my Samsung Galaxy 4 10.1 with 5.0.2. Android OS.
The Question now is how can I still say to save it on the SD-Card ?
For Example:
private File makeFolder(String folder) {
File dir = new File(Environment.getExternalStorageDirectory() + "/" + folder + "/");
if (!(dir.exists() && dir.isDirectory())) {
dir.mkdirs();
}
return dir;
}
On my older Devices (like my Sony Ericson Pro) this works just fine. So how to solve this?
You are into a slightly complicated area of Android programming :)
I can not give you complete answer here, but I can give some pointers:
On almost all devices I've seen, Environment.getExternalStorageDirectory() will give you the path to the root directory of an emulated SDCARD - which is not a physical SDCARD. Manufacturers are free to map the physical SDCARD to this path - but usually, they emulate an SDCARD using internal memory)
And prior to Kitkat, there are no API available to even to determine the presence of a physical SDCARD.
From Kitkat onwards, you can see if a physical SDCARD is present by checking the result of appContext.getExternalFilesDirs() which will give you your app's private data directory path on all available storage media.
If a physical SDCARD is present and mounted, appContext.getExternalFilesDirs() will give you two paths are result. Then the first one is for the emulated SDCARD. You may notice that this path will contain same subpath that returned by Environment.getExternalStorageDirectory().
The following example will make this clearer. I am assuming a Samsung S4 with a real SDCARD inserted and mounted in it
The first path returned will be:
/storage/emulated/0/Android/data/com.example.yourapp/files.
The second path is interesting - as this path is your app's accessible area on the the real physical SDCARD. This path will be of the form:
/storage/extSdCard/Android/data/com.example.yourapp/files
This is the only path that you have write access in Kitkat.
So, from Kitkat onwards, from the presence of these two paths, (and added checks for mounted SDCARD), you can easily find out whether your app is running on a phone with real SDCARD or not. You can even parse the second path to get the root directory of the real SDCARD (just remove /Android/... substring from this path)
And yes, now you know why you are so restricted with real SDCARDs in Kitkat. As said in italics above, you have write access to only that directory on a physical SDCARd in Kitkat.
Now comes Marshmallow and things changes again. In Marshmallow you have a storage framework, using which you can present the user with a permission dialog box and you can tell him to grant your app complete access to whatever device or directory therein. Please search for "Intent.ACTION_OPEN_DOCUMENT_TREE" for nice tutorials.
So, in short, as of now, there is no nice way in Android to deal with SDCARD which works uniformly in all versions. Before Kitkat, it is a grey area, In Kitkat, you are severely restricted, and in Lollipop, you are stuck with a system Activity with an absurd looking GUI for permission and not so easy to use framework.
I get a warning from android lint:
Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead
I will fix this, but still would need to know:
On which (example) devices is this a problem, what other (example) paths can you get from this call? If this is not related to specific devices, to what is it specific or when would it happen?
Under what circumstances is there no /sdcard/ directory that my app could write to?
The app has the rights
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
what other (example) paths can you get from this call?
Partly, it is whatever the manufacturer wants.
Partly, it will vary based on the user account that is running your app. Please bear in mind that for a few years (since the release of Android 4.2), Android devices can have multiple user accounts (tablets starting with 4.2, phones starting with 5.0). Each user gets a distinct location for internal and external storage, and there is no guarantee as to what actual filesystem paths those will point to.
NEVER HARDCODE ROOT PATHS to internal or external storage. Always use an appropriate method for getting a root location, then use the appropriate File constructor to point to whatever you want within there.
The app has the rights
Since there is no WRITE_INTERNAL_STORAGE permission in Android, please remove it.
The SD card path is different for different Android manufacturers. So I make my own research with my friends' sdcard on their phone. And the result:
Sony XPERIA X10i (my phone)
Android 2.3.3 (Gingerbread)
path for phone internal memory: not available
path for sdcard: /mnt/sdcard/
Samsung Galaxy S3 Mini & Samsung Galaxy Young (given same result)
Android 4.2.2 (JellyBean)
path for phone internal memory: /storage/sdcard0/
path for sdcard: /storage/extSdCard/
OPPO (I don't remember what her phone type is)
Android 4.2.2 (JellyBean)
path for phone internal memory: /storage/sdcard0/external_sd/
path for sdcard: /sdcard0/
I wrote the result in a book. So my suggestion, never use hardcode for sdcard's path. Check here to know your sdcard's path.
The external storage may point other places,
you may define and mount other place as device external storage,
it a configure option, that why you have a Lint warning.
For example if the device support External SD card, than the external sdcard (getExteranlStorage()) will point to him (the exteranl sdcard will mount at /nmt/sdcard#/), top keep the internal sdcard free.
To make it clear
The /sdcard/ == /mnt/sdcard[0] -> internal sdcard
other sdcard will mount at /mnt/sdcard[1..]/ -> external sdcard
And also the manufacture of the device can call it as he wish (/sdcard/ is just a convention not a must have)
I tried to figure out the the right external storage (additional sdcard with more space ) location of an Android device.
I know from different user, that the location is not always the same.
The method Environment.getExternalStorageDirectory returns me always mnt/sdcard.
I have the following variants are reported from users:
mnt/sdcard/tflash
mnt/sdcard/external_sd
mnt/sdcard
How can I determine the real location of an external sd-card?
Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard"
No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash. Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine", at least for Android 2.x and above.
But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?
Android has no concept of "external SD", aside from external storage, as described above.
If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it.
How can I determine the real location of an external sd-card?
=> Environment.getExternalStorageDirectory() gives you the exact location of your external storage device. Path may be different because it depends on the consideration of manufacturer.
For example: I found /sdcard on some HTC devices and /mnt/sdcard on some samsung devices.
So Environment.getExternalStorageDirectory() is the correct way.
When I ran on View Pad7 used sdcard/night1/9472012051_346.mp3 with success.
If I prompt for the root directory from within the app I get /mnt/sdcard/, however Prompt(IsFileExist("/mnt/sdcard/night1/9472012051_346.mp3")); returns false.
Explorer app on tablet shows path as SD Card/night1/9472012051_346.mp3
Many tablets have their own internal memory, and this is what is mounted to /mnt/sdcard (which is symlinked to by /sdcard). In these cases, any external sdcard you install is typically at /mnt/external_sd instead
I am developing an application that needs to write files to the SD card. I am using GetExternalStoragePublicDirectory() to determine the directory to write to.
I have two phones I am developing with. On a Google Nexus S, running Android 4.0.4 (Ice cream sandwich) it is returning a directory on the SD card. However, on a Samsung Exhibit 2 running Android 2.3.5, it is writing directly to the USB storage on the phone.
Is there a way to force the SD card?
edit:
I found that getExternalStoragePublicDirectory(), and getExternalStorageDirectory() are always returning "/mnt/sdcard/". This is not actually the mount location for the sd card. On the Samsung Exhibit 2, this is the USB storage location. The card mount location is "/mnt/sdcard/external_sd". Is there a way to return this actual sd location?
I see that the camera and other apps have found a way to do it. The camera app has a "storage" setting with options "phone" and "memory card". If "memory card" is specified, images are actually stored on the sd card.
Is there a way to return this actual sd location?
No, sorry, not in the current Android SDK.
I see that the camera and other apps have found a way to do it.
Mostly, they will either be ones written by the device manufacturer, or are reading information via MediaStore, which should index both sources.
If you want to write to SD card only use
File mySdCardPath = Environment.getExternalStorageDirectory();
File dir = new File (mySdCardPath.getAbsolutePath() + "/yourDirectoryName");