diff options
author | lookshe <github@lookshe.org> | 2016-03-07 23:42:13 +0100 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2016-03-07 23:42:13 +0100 |
commit | 5eda297b145558b4bcdbd0fb94f8a2675691971f (patch) | |
tree | 684e9c2b287566c4c1ec001889b82e34a5ca29b3 /src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java | |
parent | d620dcdc575ff1ce4cbdaf78e3182f3418e2142d (diff) | |
parent | c8759af913b775645b5394eb3c1f08d8c5fe9238 (diff) |
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/utils/FileHelper.java | 42 |
1 files changed, 40 insertions, 2 deletions
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; @@ -12,15 +15,47 @@ 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 * @return the real path or <code>null</code> */ 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; } |