I'm developing with Android Studio on various emulated devices.
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
always returned false UNTIL I chose to emulate SD cards on the emulated devices.
But the documentation at http://developer.android.com/training/basics/data-storage/files.html specifically states that
"there are always two storage spaces and the API behavior is the same whether the external storage is removable or not."
This seems to indicate that external storage need not refer to an actual SD card and could be internal. Therefore, why would I need to size the emulated SD card greater than zero to get that code to work? What am I misunderstanding or missing?
Also, does context.getExternalFilesDir refer to the same directory's flags as Environment.getExternalStorageState?
Thank you,
Tony
You need to choose emulate SD cards to indicate the emulator that you have an external storage associated. And what you said is correct, an external storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage.
Yes, context.getExternalFilesDir returns the absolute path to the directory on the external filesystem as per the documentation, hence refers to the same directory's flags as Environment.getExternalStorageState
Related
Ok, I have searched the forums high and low, and am unable to find an acceptable answer to test if the SD card is properly mounted and is writable on Android 7 devices. (There are many answers, but none that actually work).
I understand that one must use Environment.getExternalStorageState(), as per my code below:
pathName=context.getFilesDir().getAbsolutePath()+"/"+SavePath;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
pathName= Objects.requireNonNull(context.getExternalFilesDir(SavePath)).getAbsolutePath();
BUT...
Regardless whether the SD card is mounted or not, this always return TRUE, simply because the SD card is mounted as an emulated resource, NOT a removable resource.
I can use Environment.isExternalStorageEmulated() to check this state, BUT it still doesn't tell me whether the SD card is available or not. (i.e.: isExternalStorageEmulated() will always return true, whether there exists an SD card or not, because it is set by the file system on startup and doesn't seem to care whether there is a physical SD card in the slot or not).
Other than trying to write to the SD card, and capturing the exception error if it doesn't exist, is there any way to tell if an SD card is present or not BEFORE I attempt to write to it?
EDIT:
After reviewing the proposed solutions, this is the best I can come up with. This seems very clunky. Any ideas on how to improve this?
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if (Environment.isExternalStorageEmulated()){
File[] storage=getExternalFilesDirs(null);
// Find the first non-emulated storage space
for (File file: storage){
if (!file.toString().contains("emulated/"))
System.out.println(file.toString());
}
}
}
Your edit answer looks good since you discard the emulated storage. As an alternative you can also count the directories. If it's greater than 1 (also discarding the emulated storage) then the storage has an SD card:
public boolean hasExternalSD() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//External Storage Emulated
if (Environment.isExternalStorageEmulated()){
if (ContextCompat.getExternalFilesDirs(context, null).length > 1) {
return true;
}
}
}
return false;
}
Both getFilesDir() and getExternalStorageDirectory() are always available on every Android device.
They have nothing to do with a removable micro SD card.
I want to get list of paths of all available storage drives for example internal memory, Memory card, usb otg drive etc..
At least give suggestions to get path of removable storage.
I am experimenting with the “Adoptable storage” introduced in Android Marshmallow and I need some guidance on the expected behaviour and how the apps should handle the “Adoptable storage”.
Format “micro SD” card as Adoptable storage by using “Format as Internal”
Once Format is done, there are 2 options given to the user as follows:
Move Now
Move Later
a. Move Now:
When this option is chosen by the user, the path returned by the getExternalFilesDirs
is “/storage/emulated/0/Android/data/PACKAGE_NAME/files” and it actually points to the file system of micro SD card.
b. Move Later:
When this option is chosen by the user, the path returned by the getExternalFilesDirs
is “/storage/emulated/0/Android/data/PACKAGE_NAME/files” and it actually points to the file system of Internal embedded memory.
In both the cases, the apps can see only one storage and the other storage is completely not accessible by the Apps to store data such as Photos, Videos, etc. Is there any way to access the storage paths of both the Internal and micro SD card when the micro SD card is formatted as “Adoptable storage” ?
Apart from this documentation ,I could not find a detailed documentation on how the apps should handle this adopted storage. Is there any API that app needs to use?
Example:
Let say, if the user phone has Internal memory of 32 GB and micro SD card of 32 GB.
If the card is formatted as “Portable storage”, both 32GB(Internal) and 32GB(micro SD card) are available to user to store data.
But If the user format the card as “Internal Memory”, the user can save data(photos/video/music,etc) only to any of this 32 GB storage location, but not to the both location. The system is providing an option of “Migrating data” between the storage locations(Internal to micro SD card and viceversa), but the apps can use only 32 GB of storage(The user effectively loses his 32 GB of storage to store Media files and can be only used to install the apps ?).
Is this the expected behaviour ?
Note: Test devices used - Moto X Play & HTC 10 - Both running Android 6.0.1
getExternalFilesDir() always points to the external storage, which may or may not be available. Use getFilesDir() to dynamically get the path to wherever the App is currently stored.
Don't store the result of this since Adoptable Storage may move your app around at any time. From the docs:
The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.
In all the devices I have had the sd card has always been mounted on the path /mnt/sdcard (this value is also returned by the function Environment.getExternalStorageDirectory().getAbsolutePath())
Can I consider it as a constant or are there cases in which it is different?
No. You can't consider as Constant its change sometime /sdcard only.So use Environment.getExternalStorageDirectory().getAbsolutePath() for path.
On samsung galaxy s2 /sdcard is phone external memory. micro sd is under /sdcard/external_sd
No , the external storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage.
I have a requirement that I need to copy some files to the SD card programatically.
I have used
Environment.getExternalStorageDirectory()
to refer the SD card but In some devices it is referring to internal memory of the device.
Then I tried "/mnt/sdcard/" this path also still referring to Internal memory of the device.
I have done some investigation and came to know that "Environment.getExternalStorageDirectory()" will refer to internal memory of the device.
But I want to always store my files in SD card for all the devices.
I just want to know the path which should always refer to the Sd card in all the devices.
Is there any hard coded way to do this..??
Please help me.
Android doesn't know anything about the way the data is physically stored (could be a SD card, a CD, a chipset, etc). The only thing it knows is about whether the storage is "internal" or "external" (more details here).
So the way you are doing is fine: if the system gives you a path to an internal chipset when you call getExternalStorageDirectory(), this means that your physical device is built that way. There is no workaround for that.