开源库地址
https://github.com/LuckSiege/PictureSelector
Android平台的图片选择器,支持从相册中获取图片、视频、音频和照片,剪切(单张或多张剪切)、压缩、主题自定义配置等功能,支持动态访问和开源图片选择适用于Android 5.0+系统的框架
一、导包及配置
//选择图片,查看图片
api 'io.github.lucksiege:pictureselector:v2.7.3-rc10'
二、使用
1、调用相机
PictureSelector.create(this)
.openCamera(PictureMimeType.ofImage())
.compress(true)
.synOrAsy(true)//同步true或异步false 压缩 默认同步
.compressSavePath(getCompressPath(mContext))//压缩图片自定义保存地址
.loadImageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
.forResult(PictureConfig.REQUEST_CAMERA);
2、调用相册
PictureCacheManager.deleteAllCacheDirRefreshFile(mContext);//清除图库缓存产生的临时文件
PictureSelector.create(this)
.openGallery(PictureMimeType.ofImage())
.compress(true)
.isPageStrategy(false)
.synOrAsy(true)//同步true或异步false 压缩 默认同步
.maxSelectNum(1)// 最大图片选择数量
.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
.isCamera(false)// 是否显示拍照按钮
.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
.compressSavePath(getCompressPath(mContext))//压缩图片自定义保存地址
.loadImageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
.forResult(PictureConfig.CHOOSE_REQUEST);
3、返回处理
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//执行这个方法需要在依附的activity上调用,否则不会执行该方法
if (resultCode == RESULT_OK) {
//相机相册返回
switch (requestCode) {
case PictureConfig.CHOOSE_REQUEST:
case PictureConfig.REQUEST_CAMERA:
List<LocalMedia> phoneList = PictureSelector.obtainMultipleResult(data);
for (LocalMedia localMedia : phoneList) {
String path = "";
if (SystemUtil.getSystemVersion().equals("10")) {
//AndroidQ 该字段仅在Android Q版本中返回
path = localMedia.getAndroidQToPath();
} else {
//真正的路径,但你不能从AndroidQ获得访问
//path = localMedia.getCompressPath();
path = localMedia.getRealPath();
}
Log.i(TAG, "onActivityResult: " + path);
if (!TextUtils.isEmpty(path)) {
//todo 处理返回的path
}
}
break;
default:
break;
}
}
}