I am facing interesting problem. I have discovered, that Samsung in it's own Android phones has two implementations of storage.
1st: /sdcard - is internal memory and /sdcard/external_sd is actually inserted memory card
2nd: /storage/sdcard0 - is internal memory and /storage/ExtSdCard is actually inserted memory card.
My app needs data to be stored in sd card, so I am facing problem, how to determine, which folder might be sd card link or not. Do you know situation of any other Android maker (LG, HTC, Sony), how to cope with external SD cards and how they are visible in android system?
It is really so simple. When you consider your samsung mobiles it is the internal storage that acts as your sdcard.
if you are looking for to save a file in Sdcard, you have to stop hard coding the path like,
/mnt/sdcard.. and so.
instead use, Environment.getExternalStorageDirectory();
This will return the path to your sdcard straight away.
And from there, add Environment.getExternalStorageDirectory()+File.separator+"your directoryname";
And this works for all brands not unique to Samsung.
Related
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?
My friend is testing my application which uses the SD card to store some settings. He has a Samsung Galaxy S2 but he just told me that he hasn't got an SD card in his device. It seems that the device created a folder in the phone's memory which simulates the existence of an SD card and that is where all app files that used the Sdcard are stored.
Is this a feature available for all Android devices? Should I consider the fact that there is no SD card on a device or should I not bother? Not sure if I should check for SD card availability in my app or not.
P.S. I've just noticed that the same goes for the emulator if I don't specify memory for the SD card.
You definitely should check for sdcard availability. On some devices it might work (as you said), but on some not, and you could get a FileNotFoundException.
So it's worth checking.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.toString().equals(
state.toString())) {
//////then do your work here////////
}
another solution: https://stackoverflow.com/a/7429264/6451573
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");
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.
I'm trying to find information on how to programmatically access the HTC Droid Incredible's supposed 8GB of Internal Storage Memory.
To determine the External Storage (SD Card) Location, I'm using the command:
android.os.Environment.getExternalStorageDirectory().
I've not been able to find any corollary for Internal phone Storage.
In examining HTC/Google Nexus One, Motorola Droid, and HTC Dream/G1 phones, there (as would be expected) does not appear to be any Mount Point for such Arbitrary Internal Storage.
On these phones the Internal Storage is divided amongst /data, /system, and /cache partitions.
The Incredible's Internal Storage is certainly not going to be located in /system. /cache is also unlikely given that it is globally non-readable/non-executable.
I'd read in reviews that the phone will still be limited to 512MB or so for apps, suggesting that it is not simply a subdirectory of /data.
I do not have access to a Droid Incredible unit myself, of course.
There is more information here: Droid Incredible storage mount points. It seems that Droid Incredible mounts its internal storage to /emmc, supposedly mounted r/w. It is not yet verified whether the WRITE_EXTERNAL_STORAGE permission is necessary in order to gain r/w access - I'm trying to get this information from Google Code
I will probably solve this by allowing the user two options to store the data:
SD-Card
Droid Incredible-only Internal storage
Edit: It seems that the WRITE_EXTERNAL_STORAGE permission is sufficient: according to droidForums both /sdcard and /emmc has the same GID of 1015.
Edit2: According to Google Group Thread the Incredible returns /sdcard as the result value of Environment.getExternalStorageDirectory(). Therefore, the user needs to decide whether he wants to use /sdcard or /emmc. The auto-detection of Incredible may be based on existence of the /emmc path.
As you're aware access to internal storage is usually limited to certain directories for each application by permissions. This is to stop one application reading data from another and from accessing system files without using the APIs. This makes sense since, for example, if you had an internet banking application you wouldn't want other apps to be able to access any of its cached data.
Each application gets to store data in a directory under /data/data. However, normally you don't specify the paths explicitly but used methods like Context.openFileOutput() which creates the file in the appropriate subdirectory of your application's directly.
I agree with you that it is unlikely that the majority of the 8GB of the Incredible's storage will be used for the /data partition.
So if they are going to have a separate partition to allow music and photos to be stored easily on the phone's internal storage then they will have to do it in a way that's compatible with existing applications. This means using Environment.getExternalStorageDirectory() but since the Incredible also supports SD cards then you're right in that it's not obvious how this might work.
There is a thread on the Android Developers Google Group discussing this exact question. Whilst there isn't an answer (at the time of writing) for how the Incredible works there's a post which says the Samsung Galaxy solves the same problem in this way:
The internal storage is mounted at /sdcard
If an SD card is also available this can be found at /sdcard/sd
This seems a sensible solution since it will be compatible with existing applications, including those which have mistakenly hard coded /sdcard instead of using Environment.getExternalStorageDirectory().
So my advice would be to use Environment.getExternalStorageDirectory() when you're looking for large areas for storage - either SD card or internal to the phone - and hope that each phone returns something sensible.
Just simply use this:
String primary_sd = System.getenv("EXTERNAL_STORAGE");
if(primary_sd != null)
Log.i("EXTERNAL_STORAGE", primary_sd);
String secondary_sd = System.getenv("SECONDARY_STORAGE");
if(secondary_sd != null)
Log.i("SECONDARY_STORAGE", secondary_sd)