How to get the internal and external sdcard path in android

前端 未结 8 538
北恋
北恋 2020-12-09 09:23

Most of the new android devices have an internal sdcard and an external sdcard. I want to make a file explorer app but I can\'t find out how to get the path to use in my app

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 10:06

    I'm not sure how general an answer this but I tested it on a motorola XT830C with Android 4.4 and on a Nexus 7 android 6.0.1. and on a Samsung SM-T530NU Android 5.0.2. I used System.getenv("SECONDARY_STORAGE") and Environment.getExternalStorageDirectory().getAbsolutePath().
    The Nexus which has no second SD card, System.getenv returns null and Envirnoment.getExterna... gives /storage/emulated/0.
    The motorola device which has an external SD card gives /storage/sdcard1 for System.getenv("SECONDARY_STORAGE") and Envirnoment.getExterna... gives /storage/emulated/0.
    The samsumg returns /storage/extSdCard for the external SD.
    In my case I am making a subdirectory on the external location and am using

     appDirectory = (System.getenv("SECONDARY_STORAGE") == null)
           ? Environment.getExternalStorageDirectory().getAbsolutePath()
           : System.getenv("SECONDARY_STORAGE");
    

    to find the sdcard. Making a subdirectory in this directory is working.
    Of course I had to set permission in the manifest file to access the external memory.
    I also have a Nook 8" color tablet. When I get a chance to test on them, I'll post if I have any problems with this approach.

提交回复
热议问题