James_Duh
James_Duh

Reputation: 1371

Use Googles FUSE to determine if SD CARD is mounted

We have looked at numerous SO post that deal with the SD CARD also the SO post which seems to be the Gold Standard Gold Standard But it deals with permissions we are not asking about permission. The question deals with finding another way to determine if the SD CARD is mounted. This question only wants to deal with SDK 23+ The article that discuss FUSE is at this link FUSE

We have tried this code that when the emulator has the SD CARD ejected returns or evaluates to TRUE. Other similar configuration from SO have also been tested. My question is not only how to detect if the SD CARD is mounted but why is this code failing? We are not sure if this code can be tested on an emulator or if a real device is needed. We feel this code failure is because of the concept of the term EXTERNAL storage not meaning an actual SD CARD but making reference to the secondary EXTERNAL storage that is internal.

       public boolean chkFORSDCARD() {
       String state = Environment.getExternalStorageState();
       if (Environment.MEDIA_MOUNTED.equals(state)) {
            System.out.println("#################### IS ####### TRUE "+state);
            return true;
       }
            System.out.println("##################### IS ###### Not Available "+state);
            return false;
   }

Upvotes: 0

Views: 102

Answers (1)

Vector
Vector

Reputation: 3235

Here is where @james_duh are getting into trouble this line of code as mentioned in your comment `THE_PATH = THE_PATH + "/Documents/"; will not work when the SD CARD is unmounted with this line of code set to [1]

 File removable = ContextCompat.getExternalFilesDirs(this, null)[1];

The solution is simple remove the THE_PATH = THE_PATH + "/Documents/"; As for testing if the SD CARD is mounted I am still working on that stay tuned

This code is not real neat but it works. Why you want it to work might be a 64K question ? ? I have tested the code and it works. What might be or concern is the words used to evaluate the path not sure they are or will remain consistent Here is the code It seems point less to check the state so you can remove that test and construct a new more suitable one I did not get that far

    public void onAvail() {

    String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED) || (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)))  {
        File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
        THE_PATH = String.valueOf(removable);
        if(THE_PATH.contains("Android")){
                System.out.println("################################### EXTERNAL Storage "+THE_PATH);
                THE_PATH = THE_PATH + "/Documents/";
        }else {
            File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
            String INTERNAL_PATH = String.valueOf(dir);

            if(INTERNAL_PATH.contains("emulated")){
                System.out.println("######################## $$$$$$$ #### Internal Storage "+INTERNAL_PATH);
            }
        }
    }
}

Upvotes: 1

Related Questions