0

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;
   }
NinjaCoder
  • 2,381
  • 3
  • 24
  • 46
James_Duh
  • 1,321
  • 11
  • 31
  • It is unclear what you are asking. That code has nothing to do with an SD card. – greenapps Nov 02 '17 at 18:48
  • @greenapps What I am asking is there a way to determine if the SD CARD is mounted? And does this code work to make that determination. It would also be nice to know if an actual device is need to test the code. I am confused because the code comes from Android Developers site that says use this code to determine is SD CARD is mounted – James_Duh Nov 02 '17 at 18:54
  • @greenapps We have looked at this SO post where Commons Ware offers good advice about the path to the SD CARD https://stackoverflow.com/questions/5694933/find-an-external-sd-card-location?rq=1 – James_Duh Nov 02 '17 at 18:58
  • Scoped Directory Access comes very close. But as with getExternalFilesDirs() code cannot distinguish between SD card and usb drive. SD card can be used in two ways since Android 6 so what do you want exactly? – greenapps Nov 02 '17 at 18:58
  • The only thing you can do is let the user indicate the SD card. You can use Scoped Directory Access or Intent.ACTION_OPEN_DOCUMENT_TREE. In both cases you will have to work with content:// schemes. The File and FileOutputStream classes cannot be used anymore then. But that is no problem as for those classes the card is not writable. I wonder what you want to do with the SD card. – greenapps Nov 02 '17 at 19:01
  • @greenapps We would like to know how to check if the SD CARD actual is mounted. The results seem to be coming from the primary external storage which Google has not bothered to differentiate between the two. Samsung has been doing this for sometime as stated in the XDA article Can we navigate up the getParentFile path ? – James_Duh Nov 02 '17 at 19:05
  • @greenapps We would like to write SQLite data to the SD CARD but if the SD CARD is not available perhaps inform the user to mount the card. We understand that if the SD CARD is not available that Android will just use internal storage – James_Duh Nov 02 '17 at 19:08
  • Why do you want to know if an SD card is mounted? Why are you talking about mounted only? Is the actual parh not much interesting. And then again? Why do you want to know? What do you want to fo with the card? – greenapps Nov 02 '17 at 19:09
  • Inform the user to mount the card? What you should do instread is telling the user that if he put an SD card in his device he then can indicate it pressing the button. – greenapps Nov 02 '17 at 19:11
  • @greenapps This is what we are doing with the SD CARD if (state.equals(Environment.MEDIA_MOUNTED)) { File removable = ContextCompat.getExternalFilesDirs(this, null)[1]; THE_PATH = String.valueOf(removable); //System.out.println("EXTERNAL PATH ====> " + THE_PATH); //THE_PATH = THE_PATH + "/Documents/"; //System.out.println("new path ====> "+THE_PATH); – James_Duh Nov 02 '17 at 19:11
  • @greenapps I am OK with your suggestion on how to inform the user and what to do the design was to not involve the user – James_Duh Nov 02 '17 at 19:13
  • You do not have to check [0] for being mounted as it will if the entry is there. – greenapps Nov 02 '17 at 19:13
  • @greenapps The code ran fine with [1] until we updated to AS 3.0 then it wanted [0] if the SD CARD was ejected That is what really started this mess – James_Duh Nov 02 '17 at 19:15
  • @greenapps Then how to rewrite this line of code File removable = ContextCompat.getExternalFilesDirs(this, null)[1] to not use the parameter [1] – James_Duh Nov 02 '17 at 19:17
  • Sorry, typo i ment [1] of course. – greenapps Nov 02 '17 at 19:48

1 Answers1

1

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);
            }
        }
    }
}
Vector
  • 3,066
  • 5
  • 27
  • 54
  • @grendel So now the question becomes what to do if the user started storing the data on the SD CARD ejected the card and reloads the app This is not an additional question just a concern to deal with and as greenapps said we may have to have a design that informs the user – Vector Nov 02 '17 at 22:20