Updates file name handling method on Android (#487)

This commit is contained in:
Miguel Ruivo 2020-12-13 16:04:55 +00:00
parent f498c22e6c
commit 692d7980f2
5 changed files with 25 additions and 29 deletions

View File

@ -1,5 +1,6 @@
## 2.1.2
Fixed desktop plugin implementation
## 2.1.3
Android: Updates file name handling method. ([#487](https://github.com/miguelpruivo/flutter_file_picker/issues/487)).
Desktop (Go): Fixed desktop plugin implementation (thank you @DenchikBY).
## 2.1.1
iOS: Fixes an issue that could result in a crash when selecting a media item twice. ([#518](https://github.com/miguelpruivo/flutter_file_picker/issues/518)).

View File

@ -8,6 +8,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.Log;
import android.webkit.MimeTypeMap;
@ -55,36 +56,30 @@ public class FileUtils {
public static String getFileName(Uri uri, final Context context) {
String result = null;
//if uri is content
if (uri.getScheme() != null && uri.getScheme().equals("content")) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
//local filesystem
int index = cursor.getColumnIndex("_data");
if (index == -1)
//google drive
{
index = cursor.getColumnIndex("_display_name");
try {
if (uri.getScheme().equals("content")) {
Cursor cursor = context.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
result = cursor.getString(index);
if (result != null) {
uri = Uri.parse(result);
} else {
return null;
}
}
} catch (final Exception ex) {
Log.e(TAG, "Failed to decode file name: " + ex.toString());
} finally {
if (cursor != null) {
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
} catch (Exception ex){
Log.e(TAG, "Failed to handle file name: " + ex.toString());
}
return uri.toString();
return result;
}
public static boolean clearCache(final Context context) {

View File

@ -1 +1 @@
0915ff87e81a3bfb122df4ced418a2b0
32b17685cd2c55cd4f786d731516419d

View File

@ -16,7 +16,7 @@ class PlatformFile {
this.bytes = data['bytes'],
this.size = data['size'];
/// The absolute path for a cached copy of this file. It can be used to create a
/// The absolute path for a cached copy of this file. It can be used to create a
/// file instance with a descriptor for the given path.
/// ```
/// final File myFile = File(platformFile.path);

View File

@ -1,7 +1,7 @@
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.
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
version: 2.1.2
version: 2.1.3
dependencies:
flutter: