一次性带你解决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没改,就先用着吧
阅读全文
0 0
- 一次性带你解决android 7.0之前所有相册选择图片问题
- android选择本地相册图片路径的问题的解决
- 一次性帮你解决毕业论文的所有排版问题
- 一次性帮你解决毕业论文的所有排版问题
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- 解决安卓7.0拍照,相册选择崩溃的问题(包括压缩图片在内)
- 一次性解决导航栏的所有问题
- Android 手机相册图片选择
- Android 从相册选择图片
- android 选择图片 相册选择 马上拍照
- Android 选择图片 相册选择 马上拍照
- Android相册解决加载大量图片卡顿问题
- android 相册选择图片 图片的压缩
- 小米手机从相册选择图片问题
- Android获取系统相册所有图片
- 【iOS】让我们一次性解决导航栏的所有问题
- 【iOS】让我们一次性解决导航栏的所有问题
- iOS 一次性解决导航栏的所有问题
- h5表格隔行换色
- RAM/DDR/ROM/FLASH/EMMC
- 只想说java很强大-indexOf
- HTML当前日期和倒计时展示
- spark windows环境下开发环境快速搭建。
- 一次性带你解决android 7.0之前所有相册选择图片问题
- Phoenix UDFs
- czl蒟蒻的模板库5——线段树
- 互联网架构为什么要做服务化?
- 最简单的方法!UEFI引导+GPT分区安装win10+Ubuntu16.04双系统
- jQuery各种动画
- czl蒟蒻的模板库6——倍增LCA
- Dbutils学习(介绍和入门)
- qt写入文件内容