简体   繁体   English

查找适用于Android的SD卡路径

[英]Find SD card path for android

My phone has its SD card path named as "/storage/external_SD" 我的手机的SD卡路径名为“ / storage / external_SD”

But I hear different phone manufacturers name their paths differently. 但是我听到不同的手机制造商对它们的命名方式有所不同。 Like SD_card , externalMemory etc.. 像SD_card,externalMemory等。

I am developing an app that opens the SD card contents when a filechooser is opened. 我正在开发一个在打开文件选择器时打开SD卡内容的应用程序。

How can I set a path to open when different brands name their sd card paths differently ? 当不同品牌的SD卡路径名称不同时,如何设置打开路径?

getExternalStorageDirectory ()将为您提供(通常)SD卡的路径,因此您可以在代码中使用它。

You are looking for the Environment class and its static method getExternalStorageDirectory . 您正在寻找Environment类及其静态方法getExternalStorageDirectory

Return the primary external storage directory. 返回主外部存储目录。
... ...
In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. 在具有多个“外部”存储目录的设备中,此目录表示用户将与之交互的“主要”外部存储。 Access to secondary storage is available through 可通过以下方式访问辅助存储

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. 不同的制造商使用不同的SD卡名称,例如在三星Tab 3的extsd中使用,而其他三星设备也使用sdcard,例如不同的制造商使用不同的名称。

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. 因此,我从我的项目中创建了一个示例示例,转到此链接,其中使用androi-dirchooser库的Android目录选择器示例 This example detect the SDcard and list all the subfolders and it also detects if the device has morethan one SDcard. 本示例检测SD卡并列出所有子文件夹,并且还检测设备是否具有多个SD卡。

Part of the code looks like this For full example goto the link Android Directory Chooser 部分代码如下所示:有关完整示例,请转到链接 Android Directory Chooser

Helper Methods 辅助方法

/**
* 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);
            }


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM