0

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.

osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • show your source code & crash logs – Vipul Jun 18 '12 at 07:17
  • Go through this post where @CommonsWare explains that there may only be one external SD card http://stackoverflow.com/questions/3591494/writing-to-internal-sd-card-on-android – Arun George Jun 18 '12 at 07:19

3 Answers3

1

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.

Community
  • 1
  • 1
Tim
  • 6,692
  • 2
  • 25
  • 30
1

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*/}
Community
  • 1
  • 1
TeraTon
  • 435
  • 6
  • 15
1

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".

user370305
  • 108,599
  • 23
  • 164
  • 151
  • This will not return if there is an EXTERNAL sd ;) – Tim Jun 18 '12 at 07:11
  • @Tim Messerschmidt - For External sd, the concept is specific for device vendor. In android only External Storage concept. – user370305 Jun 18 '12 at 07:19
  • 1
    I know - that's what I've written in my post below yours. The concept of 2 sdcards was never intended by Google. – Tim Jun 18 '12 at 07:22