PictureSelector使⽤总结1.配置
1.app中的adle
dependencies {
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
//glide版本慎⽤,⾼版本会导致闪退,具体参考github上的⽂件
implementation'com.github.bumptech.glide:glide:4.5.0'
}
复制代码
2.项⽬中的adle
allprojects {
repositories {
jcenter()
maven { url 'jitpack.io' }
}
}
复制代码
2.权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
复制代码
3.添加GridImageAdapter类
代码内容直接在github上copy,,缺少的资源⽂件也直接复制github上的。
4.fragment中添加代码:
1.添加成员变量:
int aspect_ratio_x = 0;
int aspect_ratio_y = 0;
private int maxSelectNum = 6;
private List<LocalMedia> selectList = new ArrayList<>();
private GridImageAdapter adapter;
复制代码
/**
* 添加图⽚的点击事件弹出选择拍照或选择照⽚
*/
AddPicClickListener onAddPicClickListener = new GridImageAdapter            .onAddPicClickListener() {
@Override
安卓intent用法public void onAddPicClick() {
.openGallery(PictureMimeType.ofAll())
.theme(R.style.picture_default_style)
.maxSelectNum(maxSelectNum)
.minSelectNum(1)
.selectionMode(PictureConfig.MULTIPLE)
.previewImage(true)
.previewVideo(true)
.isCamera(true)
.enableCrop(false)
press(false)
.
glideOverride(160, 160)
.previewEggs(true)
.withAspectRatio(aspect_ratio_x, aspect_ratio_y)
.isGif(false)
.openClickSound(false)
.selectionMedia(selectList)
.forResult(CHOOSE_REQUEST);
}
};
复制代码
2.在onCreateView⽅法中添加代码:
initChoosePhoto();
复制代码
fragment中initChoosePhoto()⽅法:
//    初始化图⽚选择⼯具
private void initChoosePhoto() {
//⼀⾏显⽰4张
FullyGridLayoutManager manager = new FullyGridLayoutManager(getContext(), 4, GridLayoutManager                .VERTICAL, false);
addPhotoRv.setLayoutManager(manager);
adapter = new GridImageAdapter(getContext(), onAddPicClickListener);
adapter.setList(selectList);
adapter.setSelectMax(maxSelectNum);
addPhotoRv.setAdapter(adapter);
adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position, View v) {
if (selectList.size() > 0) {
LocalMedia media = (position);
String pictureType = PictureType();
int mediaType = PictureMimeType.pictureToVideo(pictureType);
switch (mediaType) {
case 1:
// 预览图⽚可⾃定长按保存路径
/
/ate(MainActivity.this).externalPicturePreview
// (position, "/custom_file", selectList);
.externalPicturePreview(position, selectList);
break;
case 2:
// 预览视频
(Path());
break;
case 3:
/
/ 预览⾳频
(Path());
break;
}
}
}
});
}
复制代码
3.重写onActivityResult⽅法:
@Override      //接收返回的地址
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CHOOSE_REQUEST && resultCode == RESULT_OK) {
//⽤照⽚选择器后的返回
// 图⽚选择结果回调
selectList = PictureSelector.obtainMultipleResult(data);
adapter.setList(selectList);
}
复制代码
4.清空数据的写法(可配合dialog添加上去)
private void cleanAllData() {
//清空
selectList.clear();
复制代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。