From 6578ecfe0e79c1d00d2282e05e28d5a4c950b687 Mon Sep 17 00:00:00 2001 From: lookshe Date: Mon, 7 Mar 2016 23:37:51 +0100 Subject: corrected getRealPathFromUri --- .../conversationsplus/utils/FileHelper.java | 42 ++++++++++++++++++++-- .../eu/siacs/conversations/utils/FileUtils.java | 5 +-- 2 files changed, 43 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java b/src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java index 3384b54e..9772b03f 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java @@ -1,7 +1,10 @@ package de.thedevstack.conversationsplus.utils; +import android.annotation.TargetApi; +import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; +import android.provider.DocumentsContract; import android.provider.MediaStore; import de.thedevstack.conversationsplus.ConversationsPlusApplication; @@ -11,6 +14,38 @@ import de.thedevstack.conversationsplus.ConversationsPlusApplication; */ public final class FileHelper { + /** + * taken from: http://stackoverflow.com/a/29164361 + * @param uri + * @return + */ + @TargetApi(19) + private static String getRealPathFromUriLollipop(Uri uri) { + String path = null; + + String wholeID = DocumentsContract.getDocumentId(uri); + + // Split at colon, use second item in the array + String id = wholeID.split(":")[1]; + + String[] column = { MediaStore.Images.Media.DATA }; + + // where id is equal to + String sel = MediaStore.Images.Media._ID + "=?"; + + Cursor cursor = ConversationsPlusApplication.getInstance().getContentResolver(). + query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + column, sel, new String[]{ id }, null); + + int columnIndex = cursor.getColumnIndex(column[0]); + + if (cursor.moveToFirst()) { + path = cursor.getString(columnIndex); + } + cursor.close(); + return path; + } + /** * Get the real path from an Uri. * @param uri the uri to convert to the real path @@ -18,9 +53,9 @@ public final class FileHelper { */ public static String getRealPathFromUri(Uri uri) { String path = null; - if (uri.getScheme().equals("file")) { + if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return uri.getPath(); - } else if (uri.toString().startsWith("content://media/")) { + } else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { String[] projection = {MediaStore.MediaColumns.DATA}; Cursor metaCursor = ConversationsPlusApplication.getInstance().getContentResolver().query(uri, projection, null, null, null); @@ -34,6 +69,9 @@ public final class FileHelper { } } } + if (path == null) { + path = getRealPathFromUriLollipop(uri); + } return path; } diff --git a/src/main/java/eu/siacs/conversations/utils/FileUtils.java b/src/main/java/eu/siacs/conversations/utils/FileUtils.java index ad8b8640..c5d0c3b0 100644 --- a/src/main/java/eu/siacs/conversations/utils/FileUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/FileUtils.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.utils; import android.annotation.SuppressLint; +import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; @@ -76,11 +77,11 @@ public class FileUtils { } } // MediaStore (and general) - else if ("content".equalsIgnoreCase(uri.getScheme())) { + else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { return getDataColumn(context, uri, null, null); } // File - else if ("file".equalsIgnoreCase(uri.getScheme())) { + else if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return uri.getPath(); } -- cgit v1.2.3