|
|
|
@ -4,10 +4,15 @@
|
|
|
|
|
|
|
|
|
|
package app.tauri.dialog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.ContentUris
|
|
|
|
|
import android.database.Cursor
|
|
|
|
|
import android.provider.MediaStore
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
|
import android.media.MediaMetadataRetriever
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.os.Environment
|
|
|
|
|
import android.provider.DocumentsContract
|
|
|
|
|
import android.provider.OpenableColumns
|
|
|
|
|
import android.util.Base64
|
|
|
|
@ -21,8 +26,40 @@ class FilePickerUtils {
|
|
|
|
|
class FileResolution(var height: Int, var width: Int)
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
fun getPathFromUri(uri: Uri): String {
|
|
|
|
|
return uri.toString()
|
|
|
|
|
fun getPathFromUri(context: Context, uri: Uri): String? {
|
|
|
|
|
if (DocumentsContract.isDocumentUri(context, uri)) {
|
|
|
|
|
if (isExternalStorageDocument(uri)) {
|
|
|
|
|
val docId = DocumentsContract.getDocumentId(uri)
|
|
|
|
|
val split = docId.split(":")
|
|
|
|
|
return if ("primary".equals(split[0], ignoreCase = true)) {
|
|
|
|
|
"${Environment.getExternalStorageDirectory()}/${split[1]}"
|
|
|
|
|
} else {
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
} else if (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)
|
|
|
|
|
} else if (isMediaDocument(uri)) {
|
|
|
|
|
val docId = DocumentsContract.getDocumentId(uri)
|
|
|
|
|
val split = docId.split(":")
|
|
|
|
|
val contentUri: Uri? = when (split[0]) {
|
|
|
|
|
"image" -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
|
|
|
|
"video" -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
|
|
|
|
"audio" -> MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
|
|
|
|
|
else -> null
|
|
|
|
|
}
|
|
|
|
|
val selection = "_id=?"
|
|
|
|
|
val selectionArgs = arrayOf(split[1])
|
|
|
|
|
return getDataColumn(context, contentUri, selection, selectionArgs)
|
|
|
|
|
}
|
|
|
|
|
} else if ("content".equals(uri.scheme, ignoreCase = true)) {
|
|
|
|
|
return getDataColumn(context, uri, null, null)
|
|
|
|
|
} else if ("file".equals(uri.scheme, ignoreCase = true)) {
|
|
|
|
|
return uri.path
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getNameFromUri(context: Context, uri: Uri): String? {
|
|
|
|
@ -36,7 +73,7 @@ class FilePickerUtils {
|
|
|
|
|
displayName = cursor.getString(columnIdx)
|
|
|
|
|
cursor.close()
|
|
|
|
|
}
|
|
|
|
|
if (displayName == null || displayName.isEmpty()) {
|
|
|
|
|
if (displayName.isNullOrEmpty()) {
|
|
|
|
|
displayName = uri.lastPathSegment
|
|
|
|
|
}
|
|
|
|
|
return displayName
|
|
|
|
@ -162,4 +199,32 @@ class FilePickerUtils {
|
|
|
|
|
return os.toByteArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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 = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
val columnIndex = cursor.getColumnIndexOrThrow(column)
|
|
|
|
|
return cursor.getString(columnIndex)
|
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
|
}
|
|
|
|
|