SD Card Location on Android - android

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

Related

Android External SD Card Access [duplicate]

This question already has answers here:
Find location of a removable SD card
(24 answers)
Closed 6 years ago.
Long time iOS developer, advanced Android programmer (but not as advanced as I thought.. LOL)..
Trying to understand Android device storage options. On my android device I have an 14Gig SD Card.
When I write code
String strDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+ File.separator + "MyFolder";
it gets placed in a "Documents folder" on the internal device (when viewed with File Manager).
The path appears to be
storage/emulated/0
and my sd card (acording to the File Manager) is
storage/extSdCard
Why does Environment.getExternalStoragePublicDirectory give me a path to my intneral card?
What am I missing that gets me to a path to store files to the external card. I think I understand part of this is do to OS changes in SD Card access in 4.1 but not sure what I'm missing.
I've read through Environment.getExternalStorageDirectory does not return the path to the removable storage and still not clear. My app currently stores files using getExternalStoragePublicDirectory, however my users are requesting they have the option of storing it on the SDCard instead?
Why does Environment.getExternalStoragePublicDirectory give me a path to my intneral card?
Because external storage is not removable storage. Neither of those are internal storage.
however my users are requesting they have the option of storing it on the SDCard instead?
On Android 4.4+, either use the Storage Access Framework (to allow the user to choose a storage location, including removable storage, Drive/Dropbox/other cloud providers, etc.), or use getExternalFilesDirs() on Context. The latter method returns a File[] — if there are 2+ elements in the array, all but the first are locations on removable storage that you can read from and write to. You cannot write to arbitrary locations on removable storage on devices that ship with Android 4.4+.

environment.getExternalStorageDirectory() shows path which doesn't even exist

When I print the SD Storage path using Environment.getExternalStorageDirectory();, it returns storage/emulated/0
but when I use DDMS to browse SD Storage there is no such directory at all. The available directory is storage/emulated/legacy
Why Environment.getExternalStorageDirectory(); shows path which doesn't even exist ?
The path exists for your app's process.
Android, starting with 4.2, supports multiple accounts per device (originally just for tablets, now for all devices starting with 5.0). Each account gets its own distinct area for internal and external storage. The framework will return paths from methods like getExternalStorageDirectory() that are correct for the current account holder that is running your app. What these locations map to in terms of actual filesystem locations is up to Android, as part of its effort to secure access to storage.

Android external storage

After reading Android documentation and Stackoverflow discussion about all storage types,
and after viewing filesystems on a number of android devices, I'm a bit cofused.
Given the following results-
getFilesDir() - returns /data/data..
getExternalFilesDir() - returns /mnt/sdcard/Android/data/<ap_name>/files
getExternalStorageDirectory() - returns /mnt/sdcard/
And given the fact that the file system Windows shows me when I'm connecting my device using USB:
device\Card - contains DCIM, Images (folders) ...
device\Phone - contains WhatsApp, PicArts, Pou (apps folders) ...
And given the fact that when openning My Files app, the path is /sdcard/ and it contains all the apps folder (WhatsApp, PicsArts..),
There are things I do not understand, such as:
How come the Device/Phone contain what /mnt/sdcard/ contains? why isn't it contained in Device/Card?
are those results mean that all those known apps use the external storage as the default storage type for media files?
My intention is to create and maintain a directory in the android file system and to store media files in there.
I want this folder to be placed where all the other apps place their folder , in this case is /sdcard/ as shown in My Files app (or /mnt/sdcard as the full path), but I dont want it to be depend on whether or not the device have an availble external storage.
What should I do?
Few remarks regarding storage :
there is no guaranty that the absolute path for getExternalFilesDir() will be the same on all devices
there is no guaranty that the absolute path for getFilesDir() will be the same on all devices
there is no guaranty that the absolute path for getExternalStorageDirectory() will be the same on all devices
So don't rely on hardcoded absolute path in your code if you need that your app run on different devices.
Every Android device have 2 storage space (mainly for historical reason) : the internal and the external.
Major differences between them :
internal storage :
always available
case sensitive file system
not accessible from a PC connected by USB (for a normal user)
accessible with getFilesDir()
files stored there are private to your app
external storage :
not always available : especially after reboot it takes some time to mount the external storage; or on some devices, when the device is connected to a PC with an USB cable; or when the user has removed the microSD.
case insensitive file system
accessible in windows explorer when connected to a PC
accessible with the api getExternalFilesDir() (to get a directory dedicated to your app) or getExternalStorageDirectory() to get the root of the external storage.
Remark about external storage : today, many vendors use a virtual external storage. i.e. there is no removable sd-card; but instead there is a partition of the internal memory that is mounted as if it was an external storage (so even on those devices the external storage is not available just after a reboot).
I want this folder to be placed where all the other apps place their folder
There is no such place. All the other apps are free to put their data either on the internal storage or on the external storage.
How come the Device/Phone contain what /mnt/sdcard/ contains? why isn't it contained in Device/Card?
Only external storage is visible when your device is USB-connected. The path Device/Phone is choose by the the usb driver (and so not really relevant). The important thing is that under this path you only see the content of root external storage (i.e. the content of the directory returned by getExternalStorageDirectory())
In my Apps I always use:
fileMedia = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yourappdirectoryonsdcard/" + "MediaFileName.3gpp";
File outFile = new File(fileMedia);
if (outFile.exists()) {
outFile.delete();
}
and don't have problems with available external storage detecting.

Get external storage parent folder on android

I've an application on the Play Store, and a user sent me a bug report saying that on his device the external sdcard is not listed in the external storage device list, though is mounted and accessible at /mnt/external_sd
I have a dialog listing folders and start directory is Environment.getExternalStorageDirectory().
After some clicks to get to the parent folder, the /storage folder is reached and getParent() returns null.
Which starting folder should I set to have access to the full storage structure?
EDIT:
As a workaround I am adding in my app preference the ability to choose if the storage root directory should be Environment.getExternalStorageDirectory() or hardcoded /mnt.
Comment/answer if there are best alternatives.
Get external storage parent folder on android
By external storage, I am assuming that you meant micro SD card storage.
external sdcard is not listed in the external storage device list,
though is mounted and accessible at /mnt/external_sd
You cannot access SD storage by using Environment.getExternalStorageDirectory() method.
Before API level 19, there was no official way to store in SD card. But, many could do it using unofficial APIs.
Officially, one method 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 Micro SD card and Internal memory. Generally, second returned path would be storage path of micro SD card.
The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.

Why there are two folders sdcard and sdcard2 under /mnt on my android device

When using Eclipse file explorer to navigate my android directories, I saw mnt/sdcard and mnt/sdcard2, see below image:
When callingEnvironment.getExternalStorageDirectory() it returns mnt/sdcard, so I think the mnt/sdcad is the external storage , and mnt/sdcard2 is my actual SD card, is that true? And how can I use code to access files under mnt/sdcard2 ?
P.S.
It seems that I can access the external sd card directly:
File extStorageDir = Environment.getExternalStorageDirectory();
String parent = extStorageDir.getParent();
File extSdCardDir = new File(parent+"/sdcard2");
File file = new File(extSdCardDir, "DemoFile.jpg");
But I wonder the extra sd card will change name in other cases.
You are correct, getExternalStorage will return your built-in external storage. Unfortunately, as of Jelly Bean applications are no longer able to utilize the SD card if the device also has built-in storage as well as an SD card. You can try working around it through shell commands or hardcoding paths, but without root there is no reliable way to access it anymore.
This was just recently added the Android CTS, which all OEMs must comply with in order to use the Play store.
Compatibility Program Overview | Android Developers
Section 9.5 (pg. 34) of Android 4.3 Compatibility Definition
Device implementations that include multiple external storage paths
MUST NOT allow Android applications to write to the secondary external
storage.
Storage Options | Android Developers
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 (the extra storage is intended only for user-provided media
that the system scans).
Android 4.2 APIs | Android Developers
Saving data in a multi-user environment
Whenever your app saves user preferences, creates a database, or
writes a file to the user’s internal or external storage space, that
data is accessible only while running as that user.
To be certain that your app behaves properly in a multi-user
environment, do not refer to your internal app directory or external
storage location using hard-coded paths and instead always use the
appropriate APIs:
For access to internal storage, use getFilesDir(), getCacheDir(), or openFileOutput().
For access to external storage, use getExternalFilesDir() or getExternalStoragePublicDirectory().
No matter which of these APIs you use to save data for a given user,
the data will not be accessible while running as a different user.
From your app’s point of view, each user is running on a completely
separate device.

Categories

Resources