diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index 1262b88e9960..7df87de1193e 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -257,9 +257,11 @@ private static ArrayList getSdCardPaths(final Context context) { String removableStoragePath; list = new ArrayList(); File fileList[] = new File("/storage/").listFiles(); - for (File file : fileList) { - if (!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead()) { - list.add(file.getAbsolutePath()); + if (fileList != null) { + for (File file : fileList) { + if (!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead()) { + list.add(file.getAbsolutePath()); + } } } } @@ -300,8 +302,12 @@ private static ArrayList getSdCardPaths19(final Context context) if (file == null) continue; final String storageState = Environment.getStorageState(file); - if (Environment.MEDIA_MOUNTED.equals(storageState)) - result.add(getRootOfInnerSdCardFolder(externalCacheDirs[i])); + if (Environment.MEDIA_MOUNTED.equals(storageState)) { + String root = getRootOfInnerSdCardFolder(externalCacheDirs[i]); + if (root != null) { + result.add(root); + } + } } if (result.isEmpty()) return null; @@ -314,6 +320,9 @@ private static String getRootOfInnerSdCardFolder(File file) if (file == null) return null; final long totalSpace = file.getTotalSpace(); + if (totalSpace <= 0) { + return null; + } while (true) { final File parentFile = file.getParentFile(); if (parentFile == null || !parentFile.canRead()) { @@ -384,19 +393,25 @@ public void Initialize() { Log.i(TAG, "Ext storage: " + extStorageState + " " + extStorageDir); - ArrayList sdCards = getSdCardPaths(this); - - // String.join doesn't exist on old devices (???). - StringBuilder s = new StringBuilder(); - for (int i = 0; i < sdCards.size(); i++) { - String sdCard = sdCards.get(i); - Log.i(TAG, "SD card: " + sdCard); - s.append(sdCard); - if (i != sdCards.size() - 1) { - s.append(":"); + String additionalStorageDirs = ""; + try { + ArrayList sdCards = getSdCardPaths(this); + + // String.join doesn't exist on old devices (???). + StringBuilder s = new StringBuilder(); + for (int i = 0; i < sdCards.size(); i++) { + String sdCard = sdCards.get(i); + Log.i(TAG, "SD card: " + sdCard); + s.append(sdCard); + if (i != sdCards.size() - 1) { + s.append(":"); + } } + additionalStorageDirs = s.toString(); + } + catch (Exception e) { + Log.e(TAG, "Failed to get SD storage dirs: " + e.toString()); } - String additionalStorageDirs = s.toString(); Log.i(TAG, "End of storage paths");