From be5e39a44084ee82c3fe1f172cb86f12e253b64f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 15 Apr 2018 17:10:50 +0200 Subject: [PATCH] get real file name for files shared from Conversations --- .../conversations/persistance/FileBackend.java | 11 ++++++----- .../eu/siacs/conversations/utils/FileUtils.java | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 569cd7c8d..464f086b4 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -536,8 +536,7 @@ public class FileBackend { public static Uri getUriForFile(Context context, File file) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { try { - String packageId = context.getPackageName(); - return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file); + return FileProvider.getUriForFile(context, getAuthority(context), file); } catch (IllegalArgumentException e) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { throw new SecurityException(e); @@ -550,6 +549,10 @@ public class FileBackend { } } + public static String getAuthority(Context context) { + return context.getPackageName() + FILE_PROVIDER; + } + public static Uri getIndexableTakePhotoUri(Uri original) { if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) { return original; @@ -727,9 +730,7 @@ public class FileBackend { input = rotate(input, getRotation(image)); return cropCenterSquare(input, size); } - } catch (SecurityException e) { - return null; // happens for example on Android 6.0 if contacts permissions get revoked - } catch (FileNotFoundException e) { + } catch (FileNotFoundException | SecurityException e) { return null; } finally { close(is); diff --git a/src/main/java/eu/siacs/conversations/utils/FileUtils.java b/src/main/java/eu/siacs/conversations/utils/FileUtils.java index 20d605506..43e3da118 100644 --- a/src/main/java/eu/siacs/conversations/utils/FileUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/FileUtils.java @@ -11,6 +11,9 @@ import android.provider.DocumentsContract; import android.provider.MediaStore; import java.io.File; +import java.util.List; + +import eu.siacs.conversations.persistance.FileBackend; public class FileUtils { @@ -83,7 +86,13 @@ public class FileUtils { } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { - String path = getDataColumn(context, uri, null, null); + List segments = uri.getPathSegments(); + String path; + if (FileBackend.getAuthority(context).equals(uri.getAuthority()) && segments.size() > 1 && segments.get(0).equals("external")) { + path = Environment.getExternalStorageDirectory().getAbsolutePath() + uri.getPath().substring(segments.get(0).length() + 1); + } else { + path = getDataColumn(context, uri, null, null); + } if (path != null) { File file = new File(path); if (!file.canRead()) { @@ -111,7 +120,7 @@ public class FileUtils { * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, - String[] selectionArgs) { + String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; @@ -120,12 +129,12 @@ public class FileUtils { }; try { - cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null); + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } - } catch(Exception e) { + } catch (Exception e) { return null; } finally { if (cursor != null) {