在遥遥领先手机中,跳转系统文件管理器选取文件。点击最近选取文本类型文件返回的Uri是这种:content://com.android.providers.media.documents/document/document%3A12096
是document类型,而在代码中。
val docId = DocumentsContract.getDocumentId(fileUri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }
.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
}
然而MediaStore中并没有document类型的处理。
经网上冲浪,问题得到解决:
else if ("document" == type){
contentUri = MediaStore.Files.getContentUri("external");
}
方法全部代码:
fun getFileAbsolutePath(context: Activity?, fileUri: Uri?): String? {
if (context == null || fileUri == null) return null
if (DocumentsContract.isDocumentUri(
context,
fileUri
)
) {
if (isExternalStorageDocument(fileUri)) {
Log.i("CCC", "getFileAbsolutePath1: ")
val docId = DocumentsContract.getDocumentId(fileUri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
val type = split[0]
if ("primary".equals(type, ignoreCase = true)) {
return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
}
} else if (isDownloadsDocument(fileUri)) {
Log.i("CCC", "getFileAbsolutePath2: ")
val id = DocumentsContract.getDocumentId(fileUri)
val contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
java.lang.Long.valueOf(id)
)
return getDataColumn(
context,
contentUri,
null,
null
)
} else if (isMediaDocument(fileUri)) {
val docId = DocumentsContract.getDocumentId(fileUri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }
.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
}else if ("document" == type){
contentUri = MediaStore.Files.getContentUri("external");
}
val selection = MediaStore.Images.Media._ID + "=?"
val selectionArgs = arrayOf(split[1])
return getDataColumn(
context,
contentUri,
selection,
selectionArgs
)
}
} // MediaStore (and general)
else if ("content".equals(fileUri.scheme, ignoreCase = true)) {
// Return the remote address
return if (isGooglePhotosUri(fileUri)) fileUri.lastPathSegment else getDataColumn(
context,
fileUri,
null,
null
)
} else if ("file".equals(fileUri.scheme, ignoreCase = true)) {
return fileUri.path
}
return null
}
private fun getDataColumn(
context: Context,
uri: Uri?,
selection: String?,
selectionArgs: Array<String>?
): String? {
var cursor: Cursor? = null
val projection = arrayOf(MediaStore.Images.Media.DATA)
try {
cursor =
context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
return cursor.getString(index)
}
} finally {
cursor?.close()
}
return null
}