How to get the internal and external sdcard path in android

前端 未结 8 563
北恋
北恋 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:03

    Yes. Different manufacturer use different SDcard name like in Samsung Tab 3 its extsd, and other samsung devices use sdcard like this different manufacturer use different names.

    I had the same requirement as you. so i have created a sample example for you from my project goto this link Android Directory chooser example which uses the androi-dirchooser library. This example detect the SDcard and list all the subfolders and it also detects if the device has morethan one SDcard.

    Part of the code looks like this For full example goto the link Android Directory Chooser

    /**
    * Returns the path to internal storage ex:- /storage/emulated/0
     *
    * @return
     */
    private String getInternalDirectoryPath() {
    return Environment.getExternalStorageDirectory().getAbsolutePath();
     }
    
    /**
     * Returns the SDcard storage path for samsung ex:- /storage/extSdCard
     *
     * @return
     */
        private String getSDcardDirectoryPath() {
        return System.getenv("SECONDARY_STORAGE");
    }
    
    
     mSdcardLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String sdCardPath;
            /***
             * Null check because user may click on already selected buton before selecting the folder
             * And mSelectedDir may contain some wrong path like when user confirm dialog and swith back again
             */
    
            if (mSelectedDir != null && !mSelectedDir.getAbsolutePath().contains(System.getenv("SECONDARY_STORAGE"))) {
                mCurrentInternalPath = mSelectedDir.getAbsolutePath();
            } else {
                mCurrentInternalPath = getInternalDirectoryPath();
            }
            if (mCurrentSDcardPath != null) {
                sdCardPath = mCurrentSDcardPath;
            } else {
                sdCardPath = getSDcardDirectoryPath();
            }
            //When there is only one SDcard
            if (sdCardPath != null) {
                if (!sdCardPath.contains(":")) {
                    updateButtonColor(STORAGE_EXTERNAL);
                    File dir = new File(sdCardPath);
                    changeDirectory(dir);
                } else if (sdCardPath.contains(":")) {
                    //Multiple Sdcards show root folder and remove the Internal storage from that.
                    updateButtonColor(STORAGE_EXTERNAL);
                    File dir = new File("/storage");
                    changeDirectory(dir);
                }
            } else {
                //In some unknown scenario at least we can list the root folder
                updateButtonColor(STORAGE_EXTERNAL);
                File dir = new File("/storage");
                changeDirectory(dir);
            }
    
    
        }
    });
    

提交回复
热议问题