I found that using Environment object isn't reliable;e to determinate SD card. Actually it works, however sometimes tells like as no SD card, so I use just direct mount as /sdcard and it works. Now I realized that a device may have several SD cards. My device report it as external-sdcard, so I can access this card as /external-sdcard or /sdcard/external-sdcard. Now question, how to figure out type of a particular mount? I can traverse directory, but File object doesn't have any attribute telling me I am SD card. I remember regular Java provides FileSystem objects which I can use to inspect particular files and this object can tell me if a File object is simple file, or it is drive, or it is external drive. Is something like that available for Android?
AFAIK, the currently documented API only supports one SD card (actually external storage).
You can use Environment.getExternalStorageDirectory() to get the directory, and Enviroment.getExternalStorageState() to check if it is mounted.
Anything besides that seems to be (currently) unsupported.
Bearing that in mind, you could parse /proc/mounts to find out what file systems are mounted where.
Related
I'm using Bluestacks for testing my app, because I don't have Androids lying around. I'm tring to write a file to the SDCard but can't seem to figure out the path for it. I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but neither of those worked.
I've tried the following: /mnt/sdcard/ext_sd & /mnt/extSdCard but
neither of those worked.
You should not hardcode paths. Because SD card storage location or path varies from phone to phone. SD card storage location in my phone is /storage/sdcard1
Now coming to your question,
Before API level 19, there was no official API method to store in SD card. But, many could do it using unofficial libraries or APIs.
Officially, one method (getExternalFilesDirs) was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).
File[] getExternalFilesDirs (String type)
It returns absolute paths to application-specific directories on all
shared/external storage devices where the application can place
persistent files it owns. These files are internal to the application,
and not typically visible to the user as media.
That means, it will return paths to both types Storage - Internal memory and Micro SD card. Generally, second returned path would be storage path of micro SD card(but not always). So you need to check it out by executing the code with this method.
Instead of hardcoding paths, you should use this method in your app source code to get the SD card location. Then, write files to that location.
If you want to know more about storage location or paths in Android, please go through my other answer
How to get the status whether a SD card is mounted/inserted in Android device or not.
The following is always returning TRUE:
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)
There are several post related to this, but I couldn't find a manufacturer-independent solution.
Android API: Iterate through all external storages obtainable with getExternalStoragePublicDirectory, calling isExternalStorageRemovable on each of them. When you find one, ask if it is mounted with getExternalStorageState. However, it may happen that the SD card is not used for any external storage directory and so you would not find it.
fstab: Parse /etc/vold.fstab, find mount points, ask if they are removeable with isExternalStorageRemovable and check their state with getExternalStorageState. Because it does not use public API, it may not work correctly in some versions of Android, including future ones. It may not work when the SD card is not used for any external storage either.
mounts: Parse /proc/mounts and do the same as for fstab but that may not work if the SD card is not used for any external storage. Or search for any partition using vfat but that may return false positives. /proc/mounts is a public Linux API so it is present in the same format everywhere. If no SD card can be found there, it is impossible to distinguish whether the SD card is unmounted or not supported at all.
However, there's really no reason to need that (and that's why there is no simple API for it). When you want to put some content in an external storage, you should use the appropriate directory provided by getExternalStoragePublicDirectory, disregarding whether it is an SD card or not.
I have an app I made on my old phone. It creates a folder called LocationTracker. When Im stepping through the code it says the folder exists. But I cant seem to browse to it when I connect my phone to the computer. I just checked my file explorer on my phone and i can browse to the folder there as well. According to that it is under the sd card, but i just cant see it on my PC.
Im rooting around in my file explorer on my phone. There appears to be 2 sd card folders. One is called sdcard1, and that seems to be my actual sd card. The other is called sd card. I think its an emulated one. I use Environment.getExternalStorageDirectory(), i thought that returned the sd card if one was available. If that is the case why is it using the emulated folder instead of the actual sd card folder? How do I fix this?
From Android documentation:
In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through
The rest of the sentence is sadly missing. However the methods are Context.getExternalFilesDirs() and Context.getExternalMediaDirs().
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));`<br/>
I want to detect the name of the sdcard at runtime, so i can store my app-data on external storage.
The above codeline is an example of storing a mp3 to user's required location.
Maybe you are asking for:
File f = new File(Environment.getExternalStorageDirectory() +
"/" + "yourfilename");
I also recomend you to avoid writing to the root of the SD card. It is a good practice to write your aplication data into the appropriate folder:
File f = new File(Environment.getExternalStorageDirectory() +
"/Android/data/com.example.testapp/" + "yourfilename");
Obviously, you have to change the "com.example.testapp" with your app package (the package you entered when you created the project in Eclipse/Android Studio).
getExternalStorageDirectory()
in
http://developer.android.com/reference/android/os/Environment.html
use Environment.getExternalStorageState() it will give path to internal SD mount point like "/mnt/sdcard"
First, on Android 4.4+, you do not have write access to removable media (e.g., "external SD"), except for any locations on that media that might be returned by
getExternalFilesDirs() and getExternalCacheDirs().
See Dave Smith's excellent analysis of this, particularly if you want the low-level details.
Second, lest anyone quibble on whether or not removable media access is otherwise part of the Android SDK, here is Dianne Hackborn's assessment:
...keep in mind: until Android 4.4, the official Android platform has not supported SD cards at all except for two special cases: the old school storage layout where external storage is an SD card (which is still supported by the platform today), and a small feature added to Android 3.0 where it would scan additional SD cards and add them to the media provider and give apps read-only access to their files (which is also still supported in the platform today).
Android 4.4 is the first release of the platform that has actually
allowed applications to use SD cards for storage. Any access to them
prior to that was through private, unsupported APIs. We now have a
quite rich API in the platform that allows applications to make use of
SD cards in a supported way, in better ways than they have been able
to before: they can make free use of their app-specific storage area
without requiring any permissions in the app, and can access any other
files on the SD card as long as they go through the file picker, again
without needing any special permissions.
I would like to get in my file browser File reference to external disk not SD Card. I mean I know that there is method
Environment.getExternalStorageDirectory();
But to my knowledge this will get reference to SD Card. But how to get external disk (say kind of USB storage attached to Android device)? You know that device can have both SD card and external USB stick attached to them.
But how to get external disk (say kind of USB storage attached to Android device)?
Android does not support this. If particular devices do, you would have to ask their manufacturers what they recommend.
In the end I have selected following solution:
1) Enlisting all existing roots using:
File[] roots=File.listRoots();
2) check presense among those roots standard ones, i.e.
File phoneRoot=new File("//");
File sdRoot=Environment.getExternalStorageDirectory();
3) If there are some extra ones - treat them as those additional external disks/sticks
I think it's best solution, since no hacks, everything is standard and this covers possible future extensions.