Skip to content

Commit

Permalink
Merge pull request #139 from quantum-byte/fix-broken-non-primary-exte…
Browse files Browse the repository at this point in the history
…rnal-storage-document-paths

Bugfix for "non-primary" external storage provider folder uris.
  • Loading branch information
lhns authored Mar 14, 2023
2 parents c044f11 + 2066bb4 commit 20c99fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion app/src/main/java/de/lolhens/resticui/util/ASFUriHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import androidx.documentfile.provider.DocumentFile;

import java.io.File;
import java.util.Arrays;
import java.util.List;

// https://gist.github.com/asifmujteba/d89ba9074bc941de1eaa#file-asfurihelper
public class ASFUriHelper {

private static final List<String> documentUriTypes = Arrays.asList(Environment.DIRECTORY_MUSIC, Environment.DIRECTORY_PODCASTS, Environment.DIRECTORY_RINGTONES, Environment.DIRECTORY_ALARMS, Environment.DIRECTORY_NOTIFICATIONS, Environment.DIRECTORY_PICTURES, Environment.DIRECTORY_MOVIES, Environment.DIRECTORY_DOWNLOADS, Environment.DIRECTORY_DCIM, Environment.DIRECTORY_DOCUMENTS);

public static String getPath(final Context context, final Uri uri) {
if (DocumentsContract.isDocumentUri(context, uri)) { // DocumentProvider
if (isExternalStorageDocument(uri)) { // ExternalStorageProvider
Expand All @@ -23,9 +31,24 @@ public static String getPath(final Context context, final Uri uri) {
} else {
return Environment.getExternalStorageDirectory() + "/";
}
} else {
final DocumentFile documentUriRoot = DocumentFile.fromTreeUri(context, DocumentsContract.buildTreeDocumentUri(
uri.getAuthority(), type + ":"));
if (documentUriRoot != null && documentUriRoot.exists() && ASFUriHelper.documentUriTypes.contains(documentUriRoot.getName())) {
final String basePath = Environment.getExternalStoragePublicDirectory(documentUriRoot.getName()).getAbsolutePath();
final String fullPath;
if (split.length > 1)
fullPath = basePath + "/" + split[1];
else {
fullPath = basePath + "/";
}
if (new File(fullPath).exists()) {
return fullPath;
}
}

}

// TODO handle non-primary volumes
} else if (isDownloadsDocument(uri)) { // DownloadsProvider
try {
final Uri contentUri = ContentUris.withAppendedId(
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ allprojects {

task clean(type: Delete) {
delete rootProject.buildDir
}
}

0 comments on commit 20c99fb

Please sign in to comment.