打开系统文件管理器兼容android8,9,10和读取第三方文件管理的文件(微信文件管理),缺点:单选
val intent = Intent ( Intent. ACTION_GET_CONTENT)
intent. type = "*/*"
intent. addCategory ( Intent. CATEGORY_OPENABLE)
( parent. context as Activity) . startActivityForResult ( intent, myRequestCode)
根据onActivityResult回调的uri获得文件路径,android8,9,10实测通过,也可以获取第三方文件管理器(微信文件管理)的路径
package com. xxxx. app. common_work. utils
import android. content. ContentResolver
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. provider. OpenableColumns
import android. support. annotation. RequiresApi
import java. io. File
import java. io. FileOutputStream
import java. io. IOException
import java. io. InputStream
object HtFileUtils {
fun getFileAbsolutePath ( context: Context, imageUri: Uri) : String? {
if ( context == null || imageUri == null ) {
return null
}
if ( Build. VERSION. SDK_INT < Build. VERSION_CODES. KITKAT) {
return getRealFilePath ( context, imageUri)
}
if ( Build. VERSION. SDK_INT >= Build. VERSION_CODES. KITKAT && Build. VERSION. SDK_INT < 29 && DocumentsContract. isDocumentUri ( context, imageUri) ) {
if ( isExternalStorageDocument ( imageUri) ) {
val docId = DocumentsContract. getDocumentId ( imageUri)
val split = docId. split ( ":" . toRegex ( ) ) . toTypedArray ( )
val type = split[ 0 ]
if ( "primary" . equals ( type, ignoreCase = true ) ) {
return Environment. getExternalStorageDirectory ( ) . toString ( ) + "/" + split[ 1 ]