Get the right external storage from Android devices - android

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.

Related

Verifying that a File path is a SD card

I've been looking for a way to get the SD card directory for a majority of the day. I've recently found out that the function call getExternalStorageDirectory under some circumstances returns a path that is on-board memory. I am supporting API 19 and above.
The common answer stackOverflow gives to get the SD card path is
Environment.getExternalStorageDirectory().getAbsolutePath();
which is not working for my devices because the external storage returned is non-removable memory (Samsung Tab S2).
I've been able to use getExternalFilesDirs to return the list of directories, and in my testing it will return two directories, the on-board memory location for the application and the SD card that I've mounted. However the function call
isExternalStorageRemovable was not introduced until API 21. This was the clearest candidate that I could find that would have satisfied what I am trying to do, which is make sure that I am actually writing to the external SD Card.
1) Is there a way to verify if the directory returned by getExternalFilesDirs is removable or external in API 19?
2) Can it be assumed that if the only storage use cases are "Internal Memory" and "Internal Memory and SD Card" that the SD card location will always be returned second from getExternalFilesDirs as has happened in my brief testing?
I've recently found out that the function call getExternalStorageDirectory under some circumstances returns a path that is on-board memory
External storage is not removable storage. On the vast majority of Android devices, getExternalStorageDirectory() returns a location that is part of the on-board flash. That has been the case for ~7 years, since the introduction of Android 3.0.
Is there a way to verify if the directory returned by getExternalFilesDirs is removable or external in API 19?
If getExternalFilesDirs() returns 2+ items, the second and subsequent ones are on removable storage.
Can it be assumed that if the only storage use cases are "Internal Memory" and "Internal Memory and SD Card"
That is not a safe assumption.
that the SD card location will always be returned second from getExternalFilesDirs as has happened in my brief testing?
The first entry returned by getExternalFilesDirs() is the same as what is returned by getExternalFilesDir() (the older, singular form). While the vast majority of Android devices have fixed external storage, there may be some devices where external storage is actually removable. In this case, the Android SDK and framework still consider this to be external storage — after all, that is what the device manufacturer told the firmware to do. And, in this case, the location returned first from getExternalFilesDirs() would be physically removable, even if it is not what Android developers typically think of as removable storage. This is a fringe scenario; I cannot name any current device models that have external storage implemented on a removable card.
Also, bear in mind that not all removable storage is in the form of a card. USB OTG thumb drives are also removable storage, as are more traditional thumb drives for those devices that have a USB Type A port (e.g., Android TV, Samsung Galaxy S8 in a DeX dock).
which is make sure that I am actually writing to the external SD Card.
I strongly recommend that you stop thinking in terms of media. Use things like the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT) to allow the user to choose places to store content from all possibilities at their disposal: external storage, removable storage, Google drive, Windows file servers, etc.

Android Adoptable Storage - find out if internal SD is mounted

When thinking about this question I asked, I wondered: Is there really no way of finding out if a adopted SD is present?
With a Motorola G3 there were two paths returned by getExternalFilesDirs() when an external SD was present, but only one (the emulated one, thats always present) when formatting the SD as internal. Other devices seem to behave similar.
So, if I am looking to know if there is a internal SD present, where would I need to look? Is there anything (non manufacturer specific) provided, so e.g. I could check, if the user expects to use the internal emulated Environment.getExternalStorageDirectory() because he has mounted a internal SD?

Comparing sdcard paths

Device 1
/sdcard attached to real sd card
/mnt/sdcard attached to real sd card
Device 2
/sdcard attached to internal memory
/mnt/sdcard attached to internal memory
/external_storage(something like this) to real sd card
.
.
/sdcard and /mnt/sdcard seems to have the same location. Always ?
My question is, how do I know if they attached to same directory ?
I tried new File(path1).equals(new File(path2)); but it return false. Only way I see is, create a hidden file with unique id and check the existence in both paths.
PS : I'm aware of Environment.getExternalStorageDirectory(). But I need these paths for some specific purpose.
/sdcard and /mnt/sdcard are not always the same. There are many paths that can exsist such as:
/emmc
/mnt/sdcard/external_sd
/mnt/external_sd
/sdcard/sd
/mnt/sdcard/bpemmctest
/mnt/sdcard/_ExternalSD
/mnt/sdcard-ext
/mnt/Removable/MicroSD
/Removable/MicroSD
/mnt/external1
/mnt/extSdCard
/mnt/extsd
/mnt/usb_storage <-- usb flash mount
/mnt/extSdCard <-- usb flash mount
/mnt/UsbDriveA <-- usb flash mount
/mnt/UsbDriveB <-- usb flash mount
There should be no reason that you would need a direct path hard coded to the internal or external memory when they make functions available for that. Anything that you are hard coding will be the same as what you get back from the functions that you have listed only the will be for that specific devices.
This will give you a string of the absolute path to a file on the external storage:
String myPath = sdcardEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/folder file is in/file you want path to";
And this will get you the path your applications internal storage:
this.getApplicationContext().getFilesDir()
You cannot always access the external SD card from code because of the way that newer versions of android partition itself. Here is statement directly from android dev site:
It's possible that a device using a partition of the internal storage for the external storage may also offer an SD card slot. In this case, the SD card is not part of the external storage and your app cannot access it
new File("/sdcard").getCanonicalPath().equals(new File("/mnt/sdcard").getCanonicalPath())
Omitted are exception handling and other niceties. Note, however, that ObieMD5 is correct, and you should not be doing this, as you can see from the list of paths in the answer above.
Also note that this method will only resolve symlinks; if your device uses mount instead to have the same space accessible from two locations, this method will not work.
attached to real sd card
The Android SDK, at present, has no concept of "real sd card". There is external storage. The definition of where external storage resides is up to the device manufacturer.
attached to internal memory
Those paths at best refer to external storage, and at worst do not exist.
/sdcard and /mnt/sdcard seems to have the same location. Always ?
No. Those paths may not even exist on some devices, as there is no requirement that they exist, and modern devices do not use those paths. Always use methods on Environment or Context to find locations on external storage.

read/write some files from internal storage at runtime

I am developing a program that need read/write some files from sdcard or internal storage at runtime. When I mount the sdcard in my phone it run correctly but if i unmount sdcard my application raises some error.
What I have to do for phones that have not card slot like Htc one x or phones that have not any sdcard inserted? how i can write on those phones storage? I don't want write this file into /data/data/my.app.package folder. I want write somewhere that files can be read from another apps.
first I check storage status with this code and if sdcard be inserted and writable i have not any problem. but if sdcard does not exists i realy don't know what i have to do.
string _location;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
_location=Environment.getExternalStorageDirectory()+"/myfolder/";
Toast.makeText(context, _location, 1000).show();
}
else
{
_location="/myfolder/"
Toast.makeText(context, Environment.getExternalStorageDirectory().toString(), 1000).show();
}
mysync mys=new mysync(); // Asynctasc that writes some file on _location path
mys.execute(_location);
i am so confused and undortunately i haven't any phone with big internal storage. please help me.
I am developing a program that need read/write some files from sdcard or internal storage at runtime.
Here, and in your question title, you say you want to write to internal storage. Later, you say that you do not want to write to internal storage.
Which is it?
What I have to do for phones that have not card slot like Htc one x or phones that have not any sdcard inserted?
External storage != "sdcard". External storage can be whatever the device manufacturer wants, so long as it meets the terms of the Compatibility Definition Document. Hence, external storage can be removable (e.g., SD card) or not (e.g.,. dedicated portion of on-board flash). And, on Android 3.0+, external storage is merely a special subdirectory on the same partition that contains internal storage.
You only care about whether external storage is presently available or not. It should be available pretty much all of the time on Android 3.0+ devices. It will be unavailable on Android 1.x and 2.x devices if the device is plugged into a host computer and the host computer has mounted the device's external storage (e.g., as a drive letter on Windows).
if sdcard does not exists i realy don't know what i have to do.
You ask the user to please unmount their device from their host computer. Or, you decide to write to internal storage in those cases.

Environment.ExternalStorageDirectory for internal/sdcard

What exactly does Environment.ExternalStorageDirectory return? I read from the developer docs:
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
Am I correct in understanding that if my device has a large (16GB) internal memory and no SD Card, this will return the path to the internal memory?
Or, do I have to check to see if the SD card is mounted and if not, use the ApplicationContext.FilesDir?
I suppose what I am asking is, if my app needs to download 250MB of data, how do I know where to store it? - As I want to use the "correct" location.
Am I correct in understanding that if my device has a large (16GB) internal memory and no SD Card, this will return the path to the internal memory?
It will return a path to external storage. On a device with the characteristics you describe, external storage will either be:
a separate data partition of that 16GB of on-board flash (Android 2.x), or
a directory within the main data partition of that 16GB of on-board flash (Android 3.0+)
Or, do I have to check to see if the SD card is mounted
External storage may be mounted on a host PC, regardless of whether external storage is represented by an SD card, a portion of on-board flash, or pixie dust.
On Android 2.x, if external storage is mounted on a host PC, it is unavailable to your application. Hence, if you are supporting Android 2.x, you need to check whether external storage is available, regardless of how that external storage is implemented.
I suppose what I am asking is, if my app needs to download 250MB of data, how do I know where to store it?
Put it on external storage. If external storage is not available, do not download the data and tell the user to unmount their phone from their host PC. There are many Android 2.x devices which do not even have 250MB of internal storage for all apps combined, let alone for just your use.
Here is another stackoverflow thread. Hope it will work for you.

Categories

Resources