项目中常常有上传图片的功能
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
private static final int MAX_SELECT_SIZE = 9;
//图片集合
private List<String> targetSource = new ArrayList<>();
//图片选择
public static final int REQUEST_CODE_CHOOSE = 0;
private void initPicRecyclerView() {
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
mImagesForOperateAdapter = new ImagesForOperateAdapter(new ArrayList<String>(), this);
recyclerView.setAdapter(mImagesForOperateAdapter);
final GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return 1;
}
});
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, UiUtil.dip2px(this, 4), false));
mImagesForOperateAdapter.setOperateClickListener(new ImagesForOperateAdapter.OperateClickListener() {
@Override
public void add() {
onAdd();
}
@Override
public void delete(int position) {
onDelete(position);
}
@Override
public void onItemClick(List<String> list, int position) {
}
});
}
/**
* 添加图片
*/
private void onAdd() {
//最多添加3张图片
int maxSelected = MAX_SELECT_SIZE - targetSource.size();
if (maxSelected > 0) {
Matisse.from(this)
// .choose(MimeType.allOf())
.choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
.countable(true)
.capture(true)
.maxSelectable(maxSelected)
.captureStrategy(new CaptureStrategy(true, "包名.fileprovider"))
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f)
.imageEngine(new PicassoEngine())
.forResult(REQUEST_CODE_CHOOSE);
} else {
ToastUtil.showToast(this, String.format(getString(R.string.max_select_error), MAX_SELECT_SIZE));
}
}
/**
* 删除图片.先清理压缩后的残留图片,再操作
*/
private void onDelete(final int position) {
String path = targetSource.get(position);
new FileCleanTask(this, path, new FileCleanTask.FileCleanCallback() {
@Override
public void callback() {
targetSource.remove(position);
mImagesForOperateAdapter.setData(targetSource);
}
}).execute();
}
/**
* 获取图片集合
*/
public List<String> getPics() {
return targetSource;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE) {
//回调选取的图片,并且转换绝对路径
List<Uri> array = Matisse.obtainResult(data);
List<String> picPaths = parsePicUris(array);
addSourceImages(picPaths);
}
}
/**
* 批量处理图片路径:uri->string
*/
private List<String> parsePicUris(List<Uri> array) {
List<String> stringList = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
Uri uri = array.get(i);
stringList.add(PictureUtils.getPathFromUri(this, uri));
}
return stringList;
}
/**
* 添加图片刷新recyclerview
*/
private void addSourceImages(final List<String> path) {
targetSource.addAll(path);
mImagesForOperateAdapter.setData(targetSource);
}