【问题标题】:Find SD card path for android查找Android的SD卡路径
【发布时间】:2014-09-05 16:02:36
【问题描述】:

我手机的 SD 卡路径名为“/storage/external_SD”

但我听说不同的手机制造商以不同的方式命名他们的路径。比如 SD_card , externalMemory 等等。

我正在开发一个在打开文件选择器时打开 SD 卡内容的应用程序。

当不同品牌的sd卡路径命名不同时,如何设置打开路径?

【问题讨论】:

    标签: android


    【解决方案1】:

    是的。不同的制造商使用不同的 SD 卡名称,例如三星 Tab 3 它的 extsd,而其他三星设备使用不同的 SD 卡,不同的制造商使用不同的名称。

    我和你有同样的要求。所以我从我的项目中为您创建了一个示例示例转到此链接Android Directory chooser example,它使用了 androi-dirchooser 库。 此示例检测 SD 卡并列出所有子文件夹,它还检测设备是否有多个 SD 卡。

    部分代码如下所示完整示例转到链接 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);
                }
    
    
            }
        });
    

    【讨论】:

      【解决方案2】:

      getExternalStorageDirectory () 会给你(通常)一张 SD 卡的路径,所以你可以在你的代码中这样做。

      【讨论】:

        【解决方案3】:

        您正在寻找Environment 类及其静态方法getExternalStorageDirectory

        返回主要的外部存储目录。
        ...
        在具有多个“外部”存储目录的设备中,该目录代表 用户将与之交互的“主要”外部存储。进入 二级存储可通过

        【讨论】:

        • 我用过。它只提供内部存储器。模拟的更精确。此方法返回 "/storage/emulated/0" 而不是所需的 "storage/external_SD" 。所以这种方法不能解决我面临的问题。
        • 试试System.getenv("SECONDARY_STORAGE"),看看你会得到什么:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多