一次性带你解决android 7.0之前所有相册选择图片问题
2017-11-09 19:00
501 查看
首先Manifest如下
provider name就是你的package路径+FileName,你可以抽到你的拓展包里
authorites,保证不重复就好了,一般都是直接你的包名.xxx
res下建一个xml foler然后
顾名思义,context都有相应方法 path为该目录下相对位置 name随便定
具体参考:https://developer.android.com/reference/android/support/v4/content/FileProvider.html
拍照:
选图片:
图片处理onActivityResult:
ok,大功告成,7.0以下通用,之所以7.0以下是因为不确定之后会不会再次有改动,反正8.0没改,就先用着吧
<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没改,就先用着吧
相关文章推荐
- Android 多媒体应用:从相册选择照片显示(解决相册中图片显示不出来的问题)
- android选择本地相册图片路径的问题的解决
- 解决安卓7.0拍照,相册选择崩溃的问题(包括压缩图片在内)
- Android开发中调用系统相册上传图片到服务器OPPO等部分手机上出现短暂的显示桌面问题的解决方法
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- Android开发——相册拍照_03.解决相机拍照之后部分手机无法将图片保存到路径或部分手机点击相机确定无法返回问题
- Android在选择相册图片闪退解决办法
- 7.0相机的路径问题,附带裁剪功能,相册或拍照选择图片均可
- Android 关于7.0权限相册不能选择 或者联系人不能选择的问题
- 兼容Android 7.0/6.0 启动系统相机/相册 选择图片方法
- [置顶] Android 7.0 调用系统拍照,相册,剪裁,修复拍照图片旋转问题
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- 解决Android从相册中获取图片出错图片却无法裁剪问题的方法
- Android 相册选择照片或拍照获取图片系统6.0及7.0实现
- Android 7.0 图片剪切问题,选择头像上传
- 解决Android 4.4 系统图库选择图片问题
- Android相册解决加载大量图片卡顿问题
- android 7.0以上共享文件(解决调用系统照相和图片剪切出现的FileUriExposedException崩溃问题)
- 解决Android从相册中获取图片出错图片却无法裁剪问题的方法
- 选择相册中的图片,并获取图片的path 解决codova 华为P6选取相册的问题(试过很多,用此方案解决)