Accessing folder paths with %System_Variables% in Android - android

I am struggling with the following issue. In a configuration file I need to access the general path for the external memory card.
This is usually done by reading the %SDCARD% system variable.
However, the makers of my phone created two partitions of the internal memory, and the second partition is mounted as "sdcard". This results in the %SDCARD%" variable returning the path to the second partition of the internal memory, rather than the externally inserted SD Card.
The SD Card is actually mounted "sdcard-ext". Is there a system variable that returns the generic path to this volume to be used in a generic path, such as
folder = "%SDCARD-EXT%/Subfolder1/Subfolder2"
to access Subfolder2 on the external card?
Thank you in advance for the help!

This is usually done by reading the %SDCARD% system variable.
Android does not use environment variables much, and Linux does not use the % syntax, last I checked.
This results in the %SDCARD%" variable returning the path to the second partition of the internal memory, rather than the externally inserted SD Card.
That is specific to this device from this manufacturer, whatever that device and manufacturer happen to be.
The SD Card is actually mounted "sdcard-ext".
That is specific to this device from this manufacturer, whatever that device and manufacturer happen to be.
Is there a system variable that returns the generic path to this volume to be used in a generic path
That is a question that only the device manufacturer can answer, as Android does not have sdcard-ext, and environment variables, to the extent that there are any, are beyond the Android SDK.

Related

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?

When does getExternalStorageDirectory().getPath() return something else than "/sdcard/"?

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)

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.

Android devices with Environment.getExternalStorageDirectory() != /mnt/sdcard/?

Of course in my code I use Environment.getExternalStorageDirectory() instead of hardwiring /mnt/sdcard/. But I just realized that when I export data from my application via a database dump in an exchange format, file paths are /mnt/sdcard/... This may explain some strange errors that I have seen in the logs from users.
I will make the appropriate changes in the import modules. But I am curious - does any Android devices > 1.6 have Environment.getExternalStorageDirectory() != /mnt/sdcard/ ?
Actually, some for some devices the getExternalStorageDirectory() will return localization for internal memory (internal SD card). The removable SD card is located as mount point below, usually getExternalStorageDirectory()+"/sd" or getExternalStorageDirectory()+"/external_sd". Unfortunately, there is no standard for this and you won't know what kind of storage you are pointing at.
No. I haven't seen any devices with sdcard mounted to different location. And I've played with more then 10 different devices from most popular vendors.
Having said that, you shouldn't rely on this fact. Especially if you have such an easy way to get path to External storage.
I had a user of one of my apps report this, he was using Andy 2.2 IIRC but I can't remember what device he was using. The path can be found using
Environment.getExternalStorageDirectory().getPath()
I think the path my user got was /sdcard & not /mnt/sdcard like my HTC phone does.

Programmatically accessing internal storage (not SD card) on Verizon HTC Droid Incredible (Android)

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)

Categories

Resources