1

I am on developing an App which has backup functionality.I have implemented the functionality successfully but the only one area i am still having trouble is the following...

To check the External Storage availability we can use following function to make sure that the SD card is presented.

Some android model phones have only internal memory. in such cases how am i suppose to handle the issue.

public static boolean isExternalStorageWritable(){
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)){
        return true;
    }
    return false;
}

public static boolean isExternalStorageReadable(){
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
        return true;
    }
    return false;
} 

Also i am aware that this will return the location of DCIM folder.but i don't think it is a best place to store important backup files.

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

Did anyone already pioneered in this problem?

Kirk
  • 4,957
  • 2
  • 32
  • 59
  • 1
    The particular External Storage API you are using does not differentiate between removable and non-removable implementations, so you don't really have to worry about that part. But a removable one is more likely to be found unavailable than a fixed one. – Chris Stratton Aug 12 '14 at 18:43
  • Thanks for answering the question. so if the device doesn't have External SD card, System will automatically save files to the Phone memory? – Kirk Aug 12 '14 at 18:45
  • 1
    If the External Storage is implemented with fixed memory, yes. If it is implemented with an SD card and the card is missing or mounted in mass storage mode to a PC over the USB cable, then no, it won't handle that for you at all. – Chris Stratton Aug 12 '14 at 18:47
  • I hope those devices which have external fixed memory and will serve the memory spaces as mounted drive. for the End user it works exactly like SD card. so we can store anywhere, in any folder. I think i got you. again thank you so much @Chris Stratton. – Kirk Aug 12 '14 at 18:53
  • I think your readonly check is not correct. – danny117 Aug 12 '14 at 19:27
  • @danny what would you suggest? – Kirk Aug 12 '14 at 20:11
  • You have or statement with same expression on both sides. I posted the code I use below. – danny117 Aug 18 '14 at 22:33

2 Answers2

1
public class CheckStorage {
    /* Checks if external storage is available for read and write */
    static public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    static public boolean isExternalStorageReadable() {
        if (CheckStorage.isExternalStorageWritable()) {
            return true;
        }
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}
danny117
  • 5,581
  • 1
  • 26
  • 35
0

Both Environment.getExternalStorageState() and Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) will not return paths to SD card.

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.

To check the External Storage availability we can use following function to make sure that the SD card is presented.

To check whether SD card is available, you need to use getExternalStorageState(File)

Official Android reference documentation says,

Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using getExternalStorageState(File).

For more details visit this link : getExternalFilesDirs(java.lang.String)

you may think that, if both above mentioned methods will not return paths to SD card, then why is there a term External present in those method names.

Because, according to Android Official Docs/Guide External Storage means, both internal memory and microSD card Storage.

In its own words,

External Storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage.

The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.

CL.
  • 173,858
  • 17
  • 217
  • 259
AnV
  • 2,794
  • 3
  • 32
  • 43