Created
November 17, 2021 20:19
-
-
Save yogithesymbian/09b63e7530e5f7dc0d1a04baa4ac5345 to your computer and use it in GitHub Desktop.
get path open file kotlin
This file contains 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
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 | |
class URIPathHelper { | |
fun getDataColumn( | |
context: Context, uri: Uri?, selection: String?, | |
selectionArgs: Array<String>? | |
): String? { | |
var cursor: Cursor? = null | |
val column = "_data" | |
val projection = arrayOf( | |
column | |
) | |
try { | |
cursor = uri?.let { | |
context.contentResolver.query( | |
it, projection, selection, selectionArgs, | |
null | |
) | |
} | |
if (cursor != null && cursor.moveToFirst()) { | |
val index = cursor.getColumnIndexOrThrow(column) | |
return cursor.getString(index) | |
} | |
} finally { | |
cursor?.close() | |
} | |
return null | |
} | |
fun getPathFromUri(context: Context, uri: Uri): String? { | |
val isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT | |
// DocumentProvider | |
if (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
isKitKat && DocumentsContract.isDocumentUri(context, uri) | |
} else { | |
TODO("VERSION.SDK_INT < KITKAT") | |
} | |
) { | |
// ExternalStorageProvider | |
if (isExternalStorageDocument(uri)) { | |
val docId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
DocumentsContract.getDocumentId(uri) | |
} else { | |
TODO("VERSION.SDK_INT < KITKAT") | |
} | |
val split = docId.split(":").toTypedArray() | |
val type = split[0] | |
if ("primary".equals(type, ignoreCase = true)) { | |
return Environment.getExternalStorageDirectory().toString() + "/" + split[1] | |
} | |
// TODO handle non-primary volumes | |
} else if (isDownloadsDocument(uri)) { | |
val id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
DocumentsContract.getDocumentId(uri) | |
} else { | |
TODO("VERSION.SDK_INT < KITKAT") | |
} | |
val contentUri = ContentUris.withAppendedId( | |
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id) | |
) | |
return getDataColumn(context, contentUri, null, null) | |
} else if (isMediaDocument(uri)) { | |
val docId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
DocumentsContract.getDocumentId(uri) | |
} else { | |
TODO("VERSION.SDK_INT < KITKAT") | |
} | |
val split = docId.split(":").toTypedArray() | |
val type = split[0] | |
var contentUri: Uri? = null | |
if ("image" == type) { | |
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | |
} else if ("video" == type) { | |
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | |
} else if ("audio" == type) { | |
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | |
} | |
val selection = "_id=?" | |
val selectionArgs = arrayOf( | |
split[1] | |
) | |
return getDataColumn(context, contentUri, selection, selectionArgs) | |
} | |
} else if ("content".equals(uri.scheme, ignoreCase = true)) { | |
// Return the remote address | |
return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn( | |
context, | |
uri, | |
null, | |
null | |
) | |
} else if ("file".equals(uri.scheme, ignoreCase = true)) { | |
return uri.path | |
} | |
return null | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is ExternalStorageProvider. | |
*/ | |
fun isExternalStorageDocument(uri: Uri): Boolean { | |
return "com.android.externalstorage.documents" == uri.authority | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is DownloadsProvider. | |
*/ | |
fun isDownloadsDocument(uri: Uri): Boolean { | |
return "com.android.providers.downloads.documents" == uri.authority | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is MediaProvider. | |
*/ | |
fun isMediaDocument(uri: Uri): Boolean { | |
return "com.android.providers.media.documents" == uri.authority | |
} | |
/** | |
* @param uri The Uri to check. | |
* @return Whether the Uri authority is Google Photos. | |
*/ | |
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