Skip to content

Commit

Permalink
Add more error checking in SD storage detection
Browse files Browse the repository at this point in the history
See #13827
  • Loading branch information
hrydgard committed Jan 8, 2021
1 parent 20eeab5 commit 17f65a2
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions android/src/org/ppsspp/ppsspp/NativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ private static ArrayList<String> getSdCardPaths(final Context context) {
String removableStoragePath;
list = new ArrayList<String>();
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());
}
}
}
}
Expand Down Expand Up @@ -300,8 +302,12 @@ private static ArrayList<String> 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;
Expand All @@ -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()) {
Expand Down Expand Up @@ -384,19 +393,25 @@ public void Initialize() {

Log.i(TAG, "Ext storage: " + extStorageState + " " + extStorageDir);

ArrayList<String> 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<String> 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");

Expand Down

0 comments on commit 17f65a2

Please sign in to comment.