Fixes getDirectoryPath (#745) and addresses a minor issue for invalid custom file types

This commit is contained in:
Miguel Ruivo 2021-07-28 22:35:11 +01:00
parent 9f0a811028
commit fc8fd7ceee
5 changed files with 57 additions and 8 deletions

View File

@ -1,3 +1,8 @@
## 3.0.4
##### Android
- Addresses an issue where an invalid custom file extension wouldn't throw an error when it should. Thank you @Jahn08.
- Fixes `getDirectoryPath()` [#745](https://github.com/miguelpruivo/flutter_file_picker/issues/745). Thank you @tomm1e.
## 3.0.3 ## 3.0.3
#### Web #### Web

View File

@ -165,7 +165,7 @@ public class FilePickerPlugin implements MethodChannel.MethodCallHandler, Flutte
allowedExtensions = FileUtils.getMimeTypes((ArrayList<String>) arguments.get("allowedExtensions")); allowedExtensions = FileUtils.getMimeTypes((ArrayList<String>) arguments.get("allowedExtensions"));
} }
if (fileType == "custom" && (allowedExtensions == null || allowedExtensions.length == 0)) { if (call.method != null && call.method.equals("custom") && (allowedExtensions == null || allowedExtensions.length == 0)) {
result.error(TAG, "Unsupported filter. Make sure that you are only using the extension without the dot, (ie., jpg instead of .jpg). This could also have happened because you are using an unsupported file extension. If the problem persists, you may want to consider using FileType.all instead.", null); result.error(TAG, "Unsupported filter. Make sure that you are only using the extension without the dot, (ie., jpg instead of .jpg). This could also have happened because you are using an unsupported file extension. If the problem persists, you may want to consider using FileType.all instead.", null);
} else { } else {
this.delegate.startFileExplorer(fileType, isMultipleSelection, withData, allowedExtensions, result); this.delegate.startFileExplorer(fileType, isMultipleSelection, withData, allowedExtensions, result);

View File

@ -6,6 +6,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
@ -176,11 +177,29 @@ public class FileUtils {
} }
@Nullable @Nullable
@SuppressWarnings("deprecation")
public static String getFullPathFromTreeUri(@Nullable final Uri treeUri, Context con) { public static String getFullPathFromTreeUri(@Nullable final Uri treeUri, Context con) {
if (treeUri == null) { if (treeUri == null) {
return null; return null;
} }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (isDownloadsDocument(treeUri)) {
String docId = DocumentsContract.getDocumentId(treeUri);
String extPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
if (docId.equals("downloads")) {
return extPath;
} else if (docId.matches("^ms[df]\\:.*")) {
String fileName = getFileName(treeUri, con);
return extPath + "/" + fileName;
} else if (docId.startsWith("raw:")) {
String rawPath = docId.split(":")[1];
return rawPath;
}
return null;
}
}
String volumePath = getVolumePath(getVolumeIdFromTreeUri(treeUri), con); String volumePath = getVolumePath(getVolumeIdFromTreeUri(treeUri), con);
FileInfo.Builder fileInfo = new FileInfo.Builder(); FileInfo.Builder fileInfo = new FileInfo.Builder();
@ -208,6 +227,24 @@ public class FileUtils {
} }
} }
@Nullable
private static String getDirectoryPath(Class<?> storageVolumeClazz, Object storageVolumeElement) {
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Method getPath = storageVolumeClazz.getMethod("getPath");
return (String) getPath.invoke(storageVolumeElement);
}
Method getDirectory = storageVolumeClazz.getMethod("getDirectory");
File f = (File) getDirectory.invoke(storageVolumeElement);
if (f != null)
return f.getPath();
} catch (Exception ex) {
return null;
}
return null;
}
@SuppressLint("ObsoleteSdkInt") @SuppressLint("ObsoleteSdkInt")
private static String getVolumePath(final String volumeId, Context context) { private static String getVolumePath(final String volumeId, Context context) {
@ -218,9 +255,10 @@ public class FileUtils {
Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getUuid = storageVolumeClazz.getMethod("getUuid"); Method getUuid = storageVolumeClazz.getMethod("getUuid");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isPrimary = storageVolumeClazz.getMethod("isPrimary"); Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
Object result = getVolumeList.invoke(mStorageManager); Object result = getVolumeList.invoke(mStorageManager);
if (result == null)
return null;
final int length = Array.getLength(result); final int length = Array.getLength(result);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
@ -229,12 +267,14 @@ public class FileUtils {
Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement); Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);
// primary volume? // primary volume?
if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) if (primary != null && PRIMARY_VOLUME_NAME.equals(volumeId)) {
return (String) getPath.invoke(storageVolumeElement); return getDirectoryPath(storageVolumeClazz, storageVolumeElement);
}
// other volumes? // other volumes?
if (uuid != null && uuid.equals(volumeId)) if (uuid != null && uuid.equals(volumeId)) {
return (String) getPath.invoke(storageVolumeElement); return getDirectoryPath(storageVolumeClazz, storageVolumeElement);
}
} }
// not found. // not found.
return null; return null;
@ -243,6 +283,10 @@ public class FileUtils {
} }
} }
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static String getVolumeIdFromTreeUri(final Uri treeUri) { private static String getVolumeIdFromTreeUri(final Uri treeUri) {
final String docId = DocumentsContract.getTreeDocumentId(treeUri); final String docId = DocumentsContract.getTreeDocumentId(treeUri);

View File

@ -1,4 +1,4 @@
import 'package:file_picker_example/src/file_picker_demo.dart'; import 'package:file_picker_example/src/file_picker_demo.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
void main() => runApp(new FilePickerDemo()); void main() => runApp(FilePickerDemo());

View File

@ -1,7 +1,7 @@
name: file_picker name: file_picker
description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support.
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
version: 3.0.3 version: 3.0.4
dependencies: dependencies:
flutter: flutter: