Last active
March 4, 2019 06:37
-
-
Save ZHocean123/8f3f853dad7343d1945b74952a4b0f8c to your computer and use it in GitHub Desktop.
getRealPathFromUri (when accessing a content in android memory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public String getRealPathFromUri(final Uri uri) { | |
| // DocumentProvider | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(this, uri)) { | |
| // ExternalStorageProvider | |
| if (isExternalStorageDocument(uri)) { | |
| final String docId = DocumentsContract.getDocumentId(uri); | |
| final String[] split = docId.split(":"); | |
| final String type = split[0]; | |
| if ("primary".equalsIgnoreCase(type)) { | |
| return Environment.getExternalStorageDirectory() + "/" + split[1]; | |
| } | |
| } | |
| // DownloadsProvider | |
| else if (isDownloadsDocument(uri)) { | |
| final String id = DocumentsContract.getDocumentId(uri); | |
| final Uri contentUri = ContentUris.withAppendedId( | |
| Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); | |
| return getDataColumn(this, contentUri, null, null); | |
| } | |
| // MediaProvider | |
| else if (isMediaDocument(uri)) { | |
| final String docId = DocumentsContract.getDocumentId(uri); | |
| final String[] split = docId.split(":"); | |
| final String type = split[0]; | |
| Uri contentUri = null; | |
| if ("image".equals(type)) { | |
| contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | |
| } else if ("video".equals(type)) { | |
| contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; | |
| } else if ("audio".equals(type)) { | |
| contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; | |
| } | |
| final String selection = "_id=?"; | |
| final String[] selectionArgs = new String[]{ | |
| split[1] | |
| }; | |
| return getDataColumn(this, contentUri, selection, selectionArgs); | |
| } | |
| } | |
| // MediaStore (and general) | |
| else if ("content".equalsIgnoreCase(uri.getScheme())) { | |
| // Return the remote address | |
| if (isGooglePhotosUri(uri)) | |
| return uri.getLastPathSegment(); | |
| return getDataColumn(this, uri, null, null); | |
| } | |
| // File | |
| else if ("file".equalsIgnoreCase(uri.getScheme())) { | |
| return uri.getPath(); | |
| } | |
| return null; | |
| } | |
| private String getDataColumn(Context context, Uri uri, String selection, | |
| String[] selectionArgs) { | |
| Cursor cursor = null; | |
| final String column = "_data"; | |
| final String[] projection = { | |
| column | |
| }; | |
| try { | |
| cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, | |
| null); | |
| if (cursor != null && cursor.moveToFirst()) { | |
| final int index = cursor.getColumnIndexOrThrow(column); | |
| return cursor.getString(index); | |
| } | |
| } finally { | |
| if (cursor != null) | |
| cursor.close(); | |
| } | |
| return null; | |
| } | |
| private boolean isExternalStorageDocument(Uri uri) { | |
| return "com.android.externalstorage.documents".equals(uri.getAuthority()); | |
| } | |
| private boolean isDownloadsDocument(Uri uri) { | |
| return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | |
| } | |
| private boolean isMediaDocument(Uri uri) { | |
| return "com.android.providers.media.documents".equals(uri.getAuthority()); | |
| } | |
| private boolean isGooglePhotosUri(Uri uri) { | |
| return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.wsights.trailer.utils | |
| import android.content.ContentUris | |
| import android.content.Context | |
| import android.database.Cursor | |
| import android.net.Uri | |
| import android.os.Build | |
| import android.os.Environment | |
| import android.provider.DocumentsContract | |
| import android.provider.MediaStore | |
| fun getRealPathFromUri(context: Context, uri: Uri): String? { | |
| when { | |
| // DocumentProvider | |
| Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri) -> { | |
| when { | |
| // ExternalStorageProvider | |
| isExternalStorageDocument(uri) -> { | |
| val docId = DocumentsContract.getDocumentId(uri) | |
| val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
| val type = split[0] | |
| if ("primary".equals(type, ignoreCase = true)) { | |
| return Environment.getExternalStorageDirectory().absolutePath + "/" + split[1] | |
| } | |
| } | |
| // DownloadsProvider | |
| isDownloadsDocument(uri) -> { | |
| val id = DocumentsContract.getDocumentId(uri) | |
| val contentUri = ContentUris.withAppendedId( | |
| Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id) | |
| ) | |
| return getDataColumn(context, contentUri, null, null) | |
| } | |
| // MediaProvider | |
| isMediaDocument(uri) -> { | |
| val docId = DocumentsContract.getDocumentId(uri) | |
| val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
| val type = split[0] | |
| var contentUri: Uri? = null | |
| when (type) { | |
| "image" -> contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | |
| "video" -> contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | |
| "audio" -> contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | |
| } | |
| val selection = "_id=?" | |
| val selectionArgs = arrayOf(split[1]) | |
| return getDataColumn(context, contentUri, selection, selectionArgs) | |
| } | |
| } | |
| } | |
| // MediaStore (and general) | |
| "content".equals(uri.scheme, ignoreCase = true) -> { | |
| // Return the remote address | |
| return if (isGooglePhotosUri(uri)) { | |
| uri.lastPathSegment | |
| } else { | |
| getDataColumn(context, uri, null, null) | |
| } | |
| } | |
| // File | |
| "file".equals(uri.scheme, ignoreCase = true) -> { | |
| return uri.path | |
| } | |
| } | |
| return null | |
| } | |
| private fun getDataColumn( | |
| context: Context, uri: Uri?, selection: String?, | |
| selectionArgs: Array<String>? | |
| ): String? { | |
| if (uri == null) return null | |
| var cursor: Cursor? = null | |
| val column = "_data" | |
| val projection = arrayOf(column) | |
| try { | |
| cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) | |
| if (cursor != null && cursor.moveToFirst()) { | |
| val index = cursor.getColumnIndexOrThrow(column) | |
| return cursor.getString(index) | |
| } | |
| } finally { | |
| cursor?.close() | |
| } | |
| return null | |
| } | |
| private fun isExternalStorageDocument(uri: Uri): Boolean { | |
| return "com.android.externalstorage.documents" == uri.authority | |
| } | |
| private fun isDownloadsDocument(uri: Uri): Boolean { | |
| return "com.android.providers.downloads.documents" == uri.authority | |
| } | |
| private fun isMediaDocument(uri: Uri): Boolean { | |
| return "com.android.providers.media.documents" == uri.authority | |
| } | |
| private fun isGooglePhotosUri(uri: Uri): Boolean { | |
| return "com.google.android.apps.photos.content" == uri.authority | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment