Skip to content

Commit

Permalink
Return correct paths for Uris that use the Vespucci file provider
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Oct 29, 2023
1 parent 5136d5b commit 25d76b4
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/main/java/de/blau/android/util/ContentResolverUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.blau.android.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.ContentUris;
Expand Down Expand Up @@ -29,6 +32,10 @@ public final class ContentResolverUtil {
private static final String RAW_PREFIX = Schemes.RAW + ":";
private static final String DOWNLOADS_DOCUMENTS = "com.android.providers.downloads.documents";
private static final String EXTERNALSTORAGE_DOCUMENTS = "com.android.externalstorage.documents";
// see filepaths.xml
private static final String ROOT = "root";
private static final String PICTURES_CAMERA = "Pictures Camera";
private static final String PICTURES = "Pictures";

/**
* Private constructor
Expand Down Expand Up @@ -58,7 +65,21 @@ public static String getPath(@NonNull Context context, @NonNull Uri uri) {
} else if (Schemes.CONTENT.equals(scheme) && context.getString(R.string.content_provider).equals(uri.getAuthority())) {
Log.i(DEBUG_TAG, "Vespucci file provider");
try {
return FileUtil.getPublicDirectory() + uri.getPath();
List<String> segments = new ArrayList<>(uri.getPathSegments());
switch (segments.get(0)) {
case PICTURES:
return FileUtil.getPublicDirectory() + pathFromSegments(segments, Paths.DELIMITER);
case PICTURES_CAMERA:
segments.remove(0);
return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), pathFromSegments(segments, Paths.DELIMITER))
.getPath();
case ROOT:
segments.remove(0);
return pathFromSegments(segments, Paths.DELIMITER);
default:
Log.e(DEBUG_TAG, "getPath unknown 1st segment");
return uri.getPath();
}
} catch (IOException e) {
Log.e(DEBUG_TAG, "getPath " + e.getMessage());
return null;
Expand All @@ -70,6 +91,23 @@ public static String getPath(@NonNull Context context, @NonNull Uri uri) {
return null;
}

/**
* Create a path from a list of path segments
*
* @param segments the list of segments
* @param sep the separator to use
* @return the path
*/
@NonNull
private static String pathFromSegments(@NonNull List<String> segments, @NonNull String sep) {
StringBuilder builder = new StringBuilder();
for (String segment : segments) {
builder.append(sep);
builder.append(segment);
}
return builder.toString();
}

/**
* Try to determine the actual file path from a Document Uri
*
Expand Down

0 comments on commit 25d76b4

Please sign in to comment.