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.