Skip to content

Commit

Permalink
fix(android): use getExternalMediaDirs api
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Aug 11, 2022
1 parent 3e78139 commit 28268bb
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions android/app/src/main/java/com/logseq/app/FileUtil.java
Expand Up @@ -40,19 +40,35 @@ public static String getPath(final Context context, final Uri uri) {
relPath = split[1];
}

String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
// attempt 1
File dir = new File("/storage/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getPath();
}
// attempt 2
dir = new File("/mnt/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getPath();
File dir = null;
File[] external = context.getExternalMediaDirs();
for (File dev : external) {
String extPath = dev.getAbsolutePath();
String devPath = extPath.substring(0, extPath.indexOf("/Android"));

if (extPath.contains("/" + type +"/")) {
dir = new File(devPath + "/" + relPath);
if (dir.exists()) {
return dir.getAbsolutePath();
}
}
}
// attempt 1
dir = new File("/storage/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getAbsolutePath();
}
// attempt 2
dir = new File("/mnt/" + type + "/" + relPath);
if (dir.exists()) {
return dir.getAbsolutePath();
}
// attempt 3
dir = new File("/mnt/media_rw" + type + "/" + relPath);
if (dir.exists()) {
return dir.getAbsolutePath();
}

// TODO: other cases
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
Expand Down

0 comments on commit 28268bb

Please sign in to comment.