From ffc989d9a32581eeb8acf563bb959cf018da1674 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Sun, 19 Jul 2020 21:35:36 +0900 Subject: [PATCH] API 30: fixed fetching volume paths on first run --- .../thibault/aves/utils/StorageUtils.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/java/deckers/thibault/aves/utils/StorageUtils.java b/android/app/src/main/java/deckers/thibault/aves/utils/StorageUtils.java index 142d5a5cf..378720aac 100644 --- a/android/app/src/main/java/deckers/thibault/aves/utils/StorageUtils.java +++ b/android/app/src/main/java/deckers/thibault/aves/utils/StorageUtils.java @@ -34,6 +34,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -119,7 +120,21 @@ public class StorageUtils { if (TextUtils.isEmpty(rawEmulatedStorageTarget)) { // fix of empty raw emulated storage on marshmallow if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - File[] files = context.getExternalFilesDirs(null); + List files; + boolean validFiles; + do { + // `getExternalFilesDirs` sometimes include `null` when called right after getting read access + // (e.g. on API 30 emulator) so we retry until the file system is ready + files = Arrays.asList(context.getExternalFilesDirs(null)); + validFiles = !files.contains(null); + if (!validFiles) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Log.e(LOG_TAG, "insomnia", e); + } + } + } while(!validFiles); for (File file : files) { String applicationSpecificAbsolutePath = file.getAbsolutePath(); String emulatedRootPath = applicationSpecificAbsolutePath.substring(0, applicationSpecificAbsolutePath.indexOf("Android/data"));