Android的5图选择举报反馈
2020-05-06 本文已影响0人
Small_Cake
1.反馈的页面
public class FeedBackActivity extends BaseBindBarActivity<ActivityFeedbackBinding> {
private List<FeedBackPicBean> datas = new ArrayList<>();
private int target_id;
private int type=1;
@Override
protected int getLayoutId() {
return R.layout.activity_feedback;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).statusBarDarkFont(true).init();
initView();
onEvent();
}
private void onEvent() {
db.tvSubmit.setOnClickListener(v -> submitLast());
}
private void initView() {
tvTitle.setText("举报");
target_id = getIntent().getIntExtra("id",0);
type = getIntent().getIntExtra("type",1);
FeedBackUtils.setImgs(this,"您可以上传截图,最多5张",db.recyclerView,datas);
}
/**
* 发起举报
* target_id 整型 必须 动态或订单ID 无
* type 整型 必须 类型 1动态(拌拌)2订单
* content 字符串 必须 举报内容 无
* images 字符串 可选 举报图片文件对象数组 无
*/
private void submitLast() {
if (EditTextUtils.isEmpty(db.etContent))return;
String content = db.etContent.getText().toString();
RequestBody idBody = RequestBody.create(MediaType.parse("text/plain"), target_id+"");
RequestBody typeBody = RequestBody.create(MediaType.parse("text/plain"), type+"");
RequestBody contentBody = RequestBody.create(MediaType.parse("text/plain"), content);
Map<String, RequestBody> map = new HashMap<>();
for (int i = 0; i < datas.size(); i++) {
FeedBackPicBean feedBackPicBean = datas.get(i);
String path = feedBackPicBean.getPath();
if (!TextUtils.isEmpty(path)){
File file = new File(path);
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
map.put("images[]\"; filename=\"" + file.getName(), fileBody);
}
}
dataProvider.appUser.report(idBody,typeBody,contentBody,map)
.subscribe(new OnSuccessAndFailListener<BaseResponse<BaseErrResponse>>(dialog) {
@Override
protected void onSuccess(BaseResponse<BaseErrResponse> baseResponse) {
BaseErrResponse data = baseResponse.getData();
String message = data.getMessage();
ToastUtil.showLong(message);
FeedBackActivity.this.finish();
}
});
}
}
2.布局activity_feedback
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/include1"
layout="@layout/action_common_bar_bill" />
<EditText
android:id="@+id/et_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="top|left"
android:hint="请尽可能详细的输入举报内容"
android:lineSpacingExtra="5dp"
android:lines="6"
android:paddingLeft="16dp"
android:paddingTop="10dp"
android:paddingRight="16dp"
android:paddingBottom="10dp"
android:textColor="@color/tv_black"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/include1" />
<androidx.recyclerview.widget.RecyclerView
android:overScrollMode="never"
android:background="@color/white"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_content"
android:clipChildren="false"
/>
<TextView
android:id="@+id/tv_submit"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginBottom="60dp"
android:background="@drawable/round_brown_gradual_bg"
android:gravity="center"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:text="提交"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3.工具类
public class FeedBackUtils {
/**
* 反馈的5图片操作
* @param context 页面
* @param desc 上传图片张数描述
* @param recyclerView 列表展示
* @param datas 要上传的图片集合
*/
public static void setImgs(Activity context,String desc, RecyclerView recyclerView, List<FeedBackPicBean> datas){
int dp16 = DpPxUtils.dp2px(16);
TextView tvU = new TextView(context);
tvU.setText(desc);
tvU.setPadding(dp16,dp16,dp16,dp16);
tvU.setTextColor(ContextCompat.getColor(context, R.color.tv_gray));
tvU.setTextSize(14);
FeedBackPicAdapter mAdapter = new FeedBackPicAdapter();
mAdapter.openLoadAnimation();
mAdapter.addData(new FeedBackPicBean(""));
mAdapter.addFooterView(tvU);
recyclerView.setLayoutManager(new GridLayoutManager(context,5));
recyclerView.setAdapter(mAdapter);
//图片的删除和添加
mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
FeedBackPicBean item = (FeedBackPicBean) adapter.getItem(position);
switch (view.getId()){
case R.id.iv_del:
mAdapter.remove(position);
List<FeedBackPicBean> data = mAdapter.getData();
if (data.size()==0||!TextUtils.isEmpty(data.get(data.size()-1).getPath())){
FeedBackPicBean feedBackPicBean = new FeedBackPicBean("");
mAdapter.addData(feedBackPicBean);
}
break;
case R.id.iv_pic:
if (TextUtils.isEmpty(item.getPath()))
getPic(context,mAdapter,datas);
break;
}
});
getPermission(context);
}
private static void getPic(Activity activity, FeedBackPicAdapter mAdapter,List<FeedBackPicBean> datas) {
int size = datas.size();
int maxSelectNum = size==0?5:(5-(size-1));
PictureParameterStyle pictureParameterStyle = new PictureParameterStyle();
pictureParameterStyle.pictureStatusBarColor = 0xFF393a3e;
pictureParameterStyle.pictureTitleBarBackgroundColor = 0xFF393a3e;
PictureSelector.create(activity)
.openGallery(PictureMimeType.ofImage())
.loadImageEngine(GlideEngine.createGlideEngine())
.isWeChatStyle(true)
.maxSelectNum(maxSelectNum)
.selectionMode(PictureConfig.MULTIPLE)
.isWithVideoImage(true)
.previewImage(true)
.isCamera(true)
.compress(true)
.compressSavePath(activity.getExternalCacheDir().getPath())
.setPictureStyle(pictureParameterStyle)
.forResult(result -> {
if (datas.size()>0){
String path = datas.get(datas.size() - 1).getPath();
if (TextUtils.isEmpty(path))datas.remove(datas.size()-1);
}
for (int i = 0; i < result.size(); i++) {
LocalMedia localMedia = result.get(i);
String androidQToPath = localMedia.getCompressPath();
datas.add(new FeedBackPicBean(androidQToPath));
}
if (datas.size()<5)datas.add(new FeedBackPicBean(""));
mAdapter.setNewData(datas);
});
}
private static void getPermission(Activity activity) {
AndPermission.with(activity)
.runtime()
.permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
.onGranted(permissions -> {
// ToastUtil.showShort("已经获取权限,可以提交反馈了。");
})
.onDenied(permissions -> {
ToastUtil.showShort("你拒绝了获取此权限!");
// 这些权限被用户总是拒绝。
if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
new AlertDialog.Builder(activity)
.setTitle("权限申请")
.setMessage("需要此权限才能使用此功能,去设置?")
.setPositiveButton("去设置", (dialog, which) -> AppUtils.goIntentSetting(activity))
.setNegativeButton("取消",null)
.show();
}
})
.start();
}
}
其中用到了图片选择器和权限申请
implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.4.6'
implementation 'com.yanzhenjie:permission:2.0.3'
image.png