一次性带你解决android 7.0之前所有相册选择图片问题

来源:互联网 发布:织梦cms重新安装 编辑:程序博客网 时间:2024/04/30 15:10

首先Manifest如下

<provider    android:name=".provider.FileProviderAndroidN"    android:authorities="com.haha.hehe"    android:exported="false"    android:grantUriPermissions="true">    <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/file_path"/></provider>
provider name就是你的package路径+FileName,你可以抽到你的拓展包里

authorites,保证不重复就好了,一般都是直接你的包名.xxx


res下建一个xml foler然后

<?xml version="1.0" encoding="utf-8"?><paths>    <external-path        name="files_root"        path="Android/data/com.haha.hehe/"/>    <files-path        name="a"        path="files"/>    <cache-path        name="b"        path="caches"/>    <external-cache-path        name="c"        path="excaches"/>    <external-files-path        name="d"        path="exfile"/></paths>
顾名思义,context都有相应方法 path为该目录下相对位置 name随便定

具体参考:https://developer.android.com/reference/android/support/v4/content/FileProvider.html


拍照:

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)intent.putExtra(MediaStore.EXTRA_OUTPUT , getUri(file))startActivityForResult(intent , CAPTURE_IMG)

fun getUri(file : File) : Uri? {    try {        return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {            FileProvider.getUriForFile(this , packageName , file)        } else {            Uri.fromFile(file)        }    } catch (e : Exception) {        e.printStackTrace()    }    return null}

选图片:

val p = Intent(Intent.ACTION_PICK ,               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)startActivityForResult(p , CHOOSE_IMG_REQUEST_CODE)


图片处理onActivityResult:

CHOOSE_IMG_REQUEST_CODE -> {    if (null == data) return    val uri = data.data    val path = getAlbumPhotoPath(uri)    if (TextUtils.isEmpty(path)) {        return    }    selectedImagePath = path}CAPTURE_IMG -> {    selectedImagePath = file.absolutePath}


fun getAlbumPhotoPath(uri : Uri) : String {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(this , uri)) {        if (isExternalStorageDocument(uri)) {            val docId = DocumentsContract.getDocumentId(uri)            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(uri)) {            val id = DocumentsContract.getDocumentId(uri)            val contentUri = ContentUris.withAppendedId(                    Uri.parse("content://downloads/public_downloads") , java.lang.Long.valueOf(id)!!)            return getDataColumn(this , contentUri , null , null)        } else if (isMediaDocument(uri)) {            val docId = DocumentsContract.getDocumentId(uri)            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            }            val selection = "_id=?"            val selectionArgs = arrayOf(split[1])            return getDataColumn(this , contentUri , selection , selectionArgs)        }    } else if ("content".equals(uri.scheme , ignoreCase = true)) {        return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn(this , uri , null , null)    } else if ("file".equals(uri.scheme , ignoreCase = true)) {        return uri.path    }    return ""}

ok,大功告成,7.0以下通用,之所以7.0以下是因为不确定之后会不会再次有改动,反正8.0没改,就先用着吧



原创粉丝点击