I am working on email application. My application will create some files in the Sd card.
I am using
Environment.getExternalStorageDirectory()
to store the files in SD card. But I have observed that some times when my device connected to my computer I am unable to access my files.
Please suggest what's the path I need to use so that my files must always accessible.
I want to store my files only in Sd card.
I think when the SD card is mounted you cannot access it.
Related
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+.
I have created one app which creates one file on external memory, but when I install it on different devices the files are created in internal sdcard in some device and not created in external(Physical) sd card.
My question is that. How do we decide between the internal or external sdcard.
Which has more preference to store file by default in android?
I use the
Environment.getExternalStorageDirectory()+ "PolicyTaskfile"+"/filename.txt";
It gives external or internal sdcard path depending on the device.
The short answer is that you do not get to pick. It's up to the OEM to decide whether external storage is truly SD card or just an internal storage device (for example: eMMC). With KitKat there is a notion of primary and secondary external storage, but no easy API to determine which you are using or which is really removable media.
I got the both the external and internal(phone memory ) SD card in listing in my application
but along with that i got some more folder and i don't want them so how can i limit to external and internal SD card only in listing...
i provided the root directory which is /storage and it displays whole the folder...
or can some one tell me how to get external sd card programmaticaly
i have tried the usual way File file = Environment.getExternalStorageDirectory();
given by android but it returned only internal path .
Environment.getExternalStorageDirectory() it returns sd card path but depends on the device
for better under standing check Find an external SD card location
According to my application, I'm downloading the pictures and some data and I need to save them somewhere in the phone memory or sd card. Since the pictures can be quite big, I decided to save it into sd card. I'm using the code below to determine whether the sd card is available or not. If it is I'm using SD card if not I'm using phone memory. But the Issue is, If I eject the SD card, my application crashes on start up and I get File Not Found exception. As far as I understand, since the phone has internal SD card,
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)
is always returning "mounted". How can I understand the external SD card is really ejected ?
EDIT: I tried to check whether the SD card is available or not , since I'm using the code below and since I have internal SD card in my phone
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)
always returns true and It creates the directory without having and SD card. So checking directory for null is not working to understand whether the file is exist or not. Here is my code below.
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir=new File(context.getExternalFilesDir(null), applicationDirectory + folderName);
}
else {
cacheDir = new File(context.getCacheDir(), applicationDirectory + folderName);
}
if(!cacheDir.isDirectory())
cacheDir.mkdirs();
So according to my solution, I try to read the required file and If I get FileNotFoundException I copy all the content from assets folder to the directory(SD card or phone memory) when application starts up and I control it with a boolean value in shared preferences. In that way if the user ejects the SD card, the content will be copied to phone memory and will be working from that directory.
I need around 10MB of cache space to save some of my files, and with this solution (I'm not sure if it's that effective) I will have my content both in SD card and phone memory if the user runs the application when there is no SD card. Any other solution to find out where to keep files, external or internal memory ? Thanks for the answers. Any Idea will be appreciated.
You could using these methods to check if your Folder on the external sd exists:
if(myFolder != null && myFolder.exists()) { /* work here */ }
myFolder would be a file targeting at the external sd (you should check the vendor mappings in the internal memory because they differ vor each vendor).
Sadly two sds is not the real Android way and this results in Android thinking the internal sd is the external one.
There is a discussion and a code example for finding the external sd here.
Check this thread:
How android application detects two SD CARDS in a device
Maybe you could then check if the folders for the sdcards exist and handle the cases where the card does not exist differently?
So something like:
sdcardloc = /mnt/sdcard/sdcard1;
if(sdcardloc != null && myFolder.exists()) { /*case where card is mounted*/ }
else { /*handle case where no card is mounted*/}
External SD card, the concept is specific for device vendor. In android only External Storage concept. And also Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage".
I am presuming that an app's private data, such as SharedPreferences and SQLite databases, live on the phone's internal storage rather than the SD card, even if the app itself is installed on the SD card.
I can't find a simple explicit confirmation of this anywhere. Can someone please confirm?
Yes, private data reside in internal storage. I've tested this by exploring file system on rooted device.
If app is "installed" on SD card, only APK file is stored on card in some encrypted form. All other app data are in /data/data// folder.