Skip to content

Instantly share code, notes, and snippets.

@yogithesymbian
Last active February 10, 2022 21:54
Show Gist options
  • Save yogithesymbian/cd5de1d6a2dfd19e7eb59850ac07ad69 to your computer and use it in GitHub Desktop.
Save yogithesymbian/cd5de1d6a2dfd19e7eb59850ac07ad69 to your computer and use it in GitHub Desktop.
uri problem on document/download
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
import android.util.Log
import java.lang.Exception
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)) {
Log.d("URIPathHelper","$FOCUS_ON_LOG FROM EXTERNAL STORAGE DOCUMENT/DOWNLOAD")
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)) {
Log.d("URIPathHelper","$FOCUS_ON_LOG FROM DOCUMENT/DOWNLOAD")
// val id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// DocumentsContract.getDocumentId(uri)
//
// } else {
// TODO("VERSION.SDK_INT < KITKAT")
// }.also {
// if (it.startsWith("raw:")) {
// return it.replaceFirst("raw:", "");
// }
// }
// val contentUri = ContentUris.withAppendedId(
// Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id)
// )
// return getDataColumn(context, contentUri, null, null)
val id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
DocumentsContract.getDocumentId(uri)
} else {
TODO("VERSION.SDK_INT < KITKAT")
}
if (id != null && id.startsWith("raw:")) {
return id.substring(4)
}
val contentUriPrefixesToTry = arrayOf(
"content://downloads/public_downloads",
"content://downloads/my_downloads",
"content://downloads/all_downloads"
)
var path: String? = null
for (contentUriPrefix in contentUriPrefixesToTry) {
val contentUri = ContentUris.withAppendedId(
Uri.parse(contentUriPrefix),
java.lang.Long.valueOf(id)
)
try {
path =
// getDataColumn(TedPermissionProvider.context, contentUri, null, null)
getDataColumn(context, contentUri, null, null)
if (path != null) {
return path
}
Log.d("URIPathHelper", "$FOCUS_ON_LOG bisa $path my prefix uri is $contentUriPrefix")
} catch (e: Exception) {
Log.d("URIPathHelper","$FOCUS_ON_LOG why kenapa $e my prefix uri is $contentUriPrefix")
}
}
return path
// val fileName: String = getFileName(TedPermissionProvider.context, uri)
// val cacheDir: File = getDocumentCacheDir(TedPermissionProvider.context)
// val file: File = generateFileName(fileName, cacheDir)
// var destinationPath: String? = null
// if (file != null) {
// destinationPath = file.getAbsolutePath()
// saveFileFromUri(TedPermissionProvider.context, uri, destinationPath)
// }
//
// return destinationPath
} else if (isMediaDocument(uri)) {
Log.d("URIPathHelper","$FOCUS_ON_LOG FROM DOCUMENT/MEDIA")
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)) {
Log.d("URIPathHelper","$FOCUS_ON_LOG FROM CONTENET/EQUAL TO")
// Return the remote address
return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn(
context,
uri,
null,
null
)
} else if ("file".equals(uri.scheme, ignoreCase = true)) {
Log.d("URIPathHelper","$FOCUS_ON_LOG FROM FILE/EQUAL TO")
return uri.path
}
Log.d("", "storage uri null there is no on condition")
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