平时我们使用手机APP都会用到照片选择器,例如微信朋友圈、淘宝评价、京东问题反馈等这些功能。PictureSelector是一款github上开源的照片选择器,可以把它集成到我们的项目中来实现我们自己的功能。效果如下:
一、首先引入PictureSelector
在app的build.gradle添加
dependencies {
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
}
在project中build.gradle加入
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
}
在AndroidManifest.xml中添加相应权限,同时添加如下代码
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.fenda.smart.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res文件下创建一个xml资源文件,再创建file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="image/" name="image" />
<external-path name="camera_photos" path="" />
</paths>
刚开时我没有添加这个<external-path name="camera_photos" path="" />打开相机的时候直接闪退
可以参考这篇博客:https://www.cnblogs.com/fengfenghuifei/p/10725603.html
准备完上面所有东西接下来就是开发我们的功能了,最上面是包含了四个radioButton,点击字体颜色和背景颜色会改变,不会的可以百度一下。中间是一个EditText同时设置了边框的颜色,接下来是用一个recyclerView来存放我们选择返回的图片,这些其实都是一些比较简单的布局。我们的重点是是怎么打开相册、打开相机, ok!
recyclerView初始化的时候有一张添加的图片,点击后弹出一个PopupWindow。我对PopupWindow上的三个按钮分别进行监听。例如点击相册则执行代码。插件会自动的打开相册
PictureSelector.create(FunctionFeedBackActivity.this)
.openGallery(PictureMimeType.ofImage())
//选择图片的最大值
.maxSelectNum(MAX_SELECT_NUM - itemCount)
//选择图片的最小值
.minSelectNum(MIN_SELECT_NUM)
//一行存放多少张
.imageSpanCount(SPAN_COUNT_NUM)
// 是否压缩
.compress(true)
.selectionMode(PictureConfig.MULTIPLE)
.forResult(PictureConfig.CHOOSE_REQUEST);
重写onActivityResult接收返回的图片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调
//images = PictureSelector.obtainMultipleResult(data);
//selectList.addAll(images);
// 例如 LocalMedia 里面返回三种path
// 1.media.getPath(); 为原图path
// 2.media.getCutPath();为裁剪后path,需判断media.isCut();是否为true
// 3.media.getCompressPath();为压缩后path,需判断media.isCompressed();是否为true
// 如果裁剪并压缩了,以取压缩路径为准,因为是先裁剪后压缩的
selectList.addAll(PictureSelector.obtainMultipleResult(data));
adapter.setList(selectList);
adapter.notifyDataSetChanged();
// uploadImg(selectList);
//打印压缩后的图片路径
for (int i = 0; i < selectList.size(); i++) {
if (selectList.get(i).isCompressed()) {
Log.e("url", selectList.get(i).getCompressPath());
}
}
}
}
}
recyclerViewAdapter如下:
public class FunctionBackImageAdapter extends RecyclerView.Adapter<FunctionBackImageAdapter.ViewHolder> {
public static final int TYPE_CAMERA = 1;
public static final int TYPE_PICTURE = 2;
private LayoutInflater mInflater;
private List<LocalMedia> list = new ArrayList<>();
private int selectMax = 9;
private Context context;
/**
* 点击添加图片跳转
*/
private onAddPicClickListener mOnAddPicClickListener;
public interface onAddPicClickListener {
void onAddPicClick();
}
public FunctionBackImageAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.mOnAddPicClickListener = mOnAddPicClickListener;
}
public void setSelectMax(int selectMax) {
this.selectMax = selectMax;
}
public void setList(List<LocalMedia> list) {
this.list = list;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImg;
LinearLayout ll_del;
TextView tv_duration;
ViewHolder(View view) {
super(view);
mImg = view.findViewById(R.id.fiv);
ll_del = view.findViewById(R.id.ll_del);
tv_duration = view.findViewById(R.id.tv_duration);
}
}
@Override
public int getItemCount() {
if (list.size() < selectMax) {
return list.size() + 1;
} else {
return list.size();
}
}
/**
* 创建ViewHolder
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = mInflater.inflate(R.layout.function_feedback_img_item_setting, viewGroup, false);
return new ViewHolder(view);
}
@Override
public int getItemViewType(int position) {
if (isShowAddItem(position)) {
return TYPE_CAMERA;
} else {
return TYPE_PICTURE;
}
}
private boolean isShowAddItem(int position) {
int size = list.size();
return positio