mirror of
https://github.com/status-im/react-native-image-crop-picker.git
synced 2025-02-23 02:48:12 +00:00
Merge pull request #262 from superandrew213/fix-resolving-external-storage-paths
Fix resolving external storage paths
This commit is contained in:
commit
a892a7e432
@ -8,6 +8,8 @@ import android.provider.DocumentsContract;
|
||||
import android.provider.MediaStore;
|
||||
import android.content.ContentUris;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import java.io.File;
|
||||
|
||||
public class RealPathUtil {
|
||||
public static String getRealPathFromURI(final Context context, final Uri uri) {
|
||||
@ -22,7 +24,23 @@ public class RealPathUtil {
|
||||
final String[] split = docId.split(":");
|
||||
final String type = split[0];
|
||||
|
||||
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||
if ("primary".equalsIgnoreCase(type)) {
|
||||
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||
} else {
|
||||
final int splitIndex = docId.indexOf(':', 1);
|
||||
final String tag = docId.substring(0, splitIndex);
|
||||
final String path = docId.substring(splitIndex + 1);
|
||||
|
||||
String nonPrimaryVolume = getPathToNonPrimaryVolume(context, tag);
|
||||
if (nonPrimaryVolume != null) {
|
||||
String result = nonPrimaryVolume + "/" + path;
|
||||
File file = new File(result);
|
||||
if (file.exists() && file.canRead()) {
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// DownloadsProvider
|
||||
else if (isDownloadsDocument(uri)) {
|
||||
@ -139,4 +157,23 @@ public class RealPathUtil {
|
||||
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.KITKAT)
|
||||
public static String getPathToNonPrimaryVolume(Context context, String tag) {
|
||||
File[] volumes = context.getExternalCacheDirs();
|
||||
if (volumes != null) {
|
||||
for (File volume : volumes) {
|
||||
if (volume != null) {
|
||||
String path = volume.getAbsolutePath();
|
||||
if (path != null) {
|
||||
int index = path.indexOf(tag);
|
||||
if (index != -1) {
|
||||
return path.substring(0, index) + tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user