Android: identify a physical SD card and write on it

1

I have a problem that I'm finding it hard to understand.

I am working on an app that should control the storage space of the device to be able to carry out its usual processes, in addition to it; the application must be able to identify if a physical SD card exists (inserted in the slot) and prioritize its use instead of the emulated external space.

I have read a lot and tried many examples and the result is the same, I detail it below:

  • Always the phone indicates that the external space is emulated
  • Even if you have an SD card in the slot, the app does not use it, does not identify it because of the above.
  • Therefore I have not found a way to calculate the space available for storage types: internal (the one used by apps / data / data), external emulated (if I do not have SD), real external (if I have SD) )
  • To save a file to the SD (supposedly) I use the sentence

    File file = new File(Environment.getExternalStorageDirectory(), name);
    

    where name is a String with the name of the file. The file is created always in the emulated external space, it does not matter if you have an SD in the slot, you do not use it. According to this , that should be the way correct

    The isExternalStorageEmulated() method always returns true and the isExternalStorageRemovable() method always returns false

    I am cured in health of the following:

    String state = Environment.getExternalStorageState();// siempre es "mounted" con SD o no presente
    if(Environment.MEDIA_MOUNTED.equals(state)) {
        ...
    }
    

    Even this:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    In all the places I have consulted, these methods do their job, but I do not understand in my case because I can not achieve the result I want.

    I have tested my app on the following devices:

  • LG L5 (Android 4 +)
  • Sony Xperia (Android 4+)
  • Alcatel Pop 2 (Android 5)
  • The result has been the same. In particular, the questions to solve would be:

  • How to identify "really" the presence of a physical SD?
  • How to "really" write a file to a physical SD?
  • asked by Rosendo Ropher 26.04.2016 в 18:33
    source

    2 answers

    1

    As far as I know, the route of the SD depends on the manufacturer. I have found these 3 routes so far: / storage / sdcard1 / / storage / extsd / / mnt / external_sd /

    It is unhelpful to do this (It works for me because I work with a few manufacturers). In this thread they speak of a function that returns the route, even though it only returns "null" to me.

    How to save and read directly on the scard

    String sdPath = System.getenv("SECONDARY_STORAGE");
    

    I hope it serves you

        
    answered by 05.05.2016 в 09:30
    0

    First you have to check if the SD Card is "mounted":

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))   {
            Log.i("SDCARDINFO","SD Card se encuentra presente.");
       } else {
            Log.i("SDCARDINFO","No se encuentra SD Card.");
       }
    

    Editing ....

        
    answered by 26.04.2016 в 18:40