簡體   English   中英

檢查SD卡可用性Android

[英]Check sd-card availablity android

在我的應用程序中,我想檢查SD卡是否存在(已安裝)。 當我嘗試運行該應用程序時,即使沒有安裝,也會出現“ SD卡已安裝”的情況。

public boolean isSDCardPresent() {

        return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

    }

即使沒有sd卡,上述代碼也始終返回true。

嘗試將您的方法更改為此

public boolean isSDCardPresent() {

    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

}

試試下面的代碼:

public boolean isSDCardPresent() { 
    return android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}

外部(可移動)SD路徑因設備而異,我也無法找出一種方法來檢查其可用性,因此我編寫了一種方法,該方法遍歷不同制造商使用的所有不同ext路徑,然后它找到完全匹配。 如果找到文件夾並且卡已插入手機,則返回true。

注意:我在方法內部使用了StreamSupport庫,因此您需要下載jar文件並將其添加到項目的libs文件夾中,這樣就可以了!

   public static boolean isExternalCardAvailable(Context context) {
    List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/");
    final List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable");
    final List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext",
            "sdext1", "sdext2", "sdext3", "sdext4");
    final String[] thePath = {""};
    Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch)
            .filter(new Predicate<String>() {
                @Override
                public boolean test(final String s) {
                    File folder = new File(s);
                    return folder.exists() && folder.isDirectory();
                }
            }) //I got the ones that exist and are directories
            .flatMap(new Function<String, Stream<File>>() {
                @Override
                public Stream<File> apply(final String s) {
                    try {
                        List<File> files = Arrays.asList(new File(s).listFiles());
                        return StreamSupport.stream(files);
                    } catch (NullPointerException e) {
                        return StreamSupport.stream(new ArrayList<File>());
                    }
                }
            }) //I got all sub-dirs of the main folders
            .flatMap(new Function<File, Stream<File>>() {
                @Override
                public Stream<File> apply(final File file1) {
                    if (listOf2DepthFolders.contains(file1.getName()
                            .toLowerCase())) {
                        try {
                            List<File> files = Arrays.asList(file1.listFiles());
                            return StreamSupport.stream(files);
                        } catch (NullPointerException e) {
                            return StreamSupport.stream(Collections.singletonList(file1));
                        }
                    } else
                        return StreamSupport.stream(Collections.singletonList(file1));
                }
            }) //Here I got all the 2 depth and 3 depth folders
            .filter(new Predicate<File>() {
                @Override
                public boolean test(final File o) {
                    return listOfExtFolders.contains(o.getName()
                            .toLowerCase());
                }
            })
            .findFirst();

    optional.ifPresent(new Consumer<File>() {
        @Override
        public void accept(final File file) {
            thePath[0] = file.getAbsolutePath();
        }
    });

    Log.e("Path", thePath[0]);

    try {
        ContextCompat.getExternalFilesDirs(context, null);
    } catch (Exception e) {
        Log.e("PathException", thePath[0]);
    }
    return !thePath[0].equals("") && new File(thePath[0]).listFiles()!=null;
}

PS我在一些HTC和Samsung設備上進行了測試和驗證。

昨天我有一個非常類似的問題:

我想檢查一下我的設備中是否有實體的SD卡。

問題是

public boolean isSDCardPresent() {

return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);}

由於模擬的sd卡,總是返回true(即使sd卡已被物理移除)。

所以這是我的解決方案:

static boolean externalStoragecheck() {
    return !Environment.isExternalStorageEmulated();
}

尚未找到類似的內容,因此我想與您分享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM