EditStudentActivity.java
14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.hjx.parent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.hjx.parent.bean.StudentBean;
import com.prws.common.base.BaseActivity;
import com.prws.common.base.BasePresenter;
import com.prws.common.bean.ResponseResult;
import com.prws.common.net.NetWorks;
import com.prws.common.utils.LogUtil;
import com.prws.common.utils.ScreenUtils;
import com.prws.common.utils.SharedPreferencesUtil;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
public class EditStudentActivity extends BaseActivity {
View view_2;
View view_3;
View view_4;
View view_5;
Button btn_next;
TextView tv_name, tv_sex, tv_nian, tv_shen;
@BindView(R.id.iv_avatar)
ImageView iv_avatar;
@BindView(R.id.tv_teacher)
TextView tv_teacher;
@BindView(R.id.iv_teacher)
ImageView iv_teacher;
private static int CAMERA_REQUEST_CODE = 343;
private static int ALBUM_REQUEST_CODE = 456;
private static int CROP_REQUEST_CODE = 234;
private File tempFile;
private String path;
StudentBean studentBean;
@Override
protected int layoutResId() {
return R.layout.activity_edit_stu;
}
@Override
public Object getContract() {
return null;
}
@Override
public BasePresenter getPresenter() {
return null;
}
@Override
protected void initView() {
view_2 = findViewById(R.id.view_2);
view_3 = findViewById(R.id.view_3);
view_4 = findViewById(R.id.view_4);
view_5 = findViewById(R.id.view_5);
tv_name = findViewById(R.id.tv_name);
tv_sex = findViewById(R.id.tv_sex);
tv_nian = findViewById(R.id.tv_nian);
tv_shen = findViewById(R.id.tv_shen);
btn_next = findViewById(R.id.btn_next);
}
@Override
protected void onStart() {
super.onStart();
String name = (String) SharedPreferencesUtil.getData("setName", "");
String sex = (String) SharedPreferencesUtil.getData("setSex", "");
String nian = (String) SharedPreferencesUtil.getData("setNian", "");
String shen = (String) SharedPreferencesUtil.getData("setShen", "");
String xueq = (String) SharedPreferencesUtil.getData("setXueq", "");
btn_next.setTextColor(Color.WHITE);
btn_next.setBackground(getResources().getDrawable(R.drawable.bg_solid_btn));
studentBean = new Gson().fromJson(getIntent().getStringExtra("student"), StudentBean.class);
if (name != null && name.length() > 0) {
studentBean.setNickName(name);
}
if (sex != null && sex.length() > 0) {
studentBean.setGender("男".equals(sex) ? 0 : 1);
}
if (nian != null && nian.length() > 0) {
studentBean.setGrade(nian);
studentBean.setGradeId((Integer) SharedPreferencesUtil.getData("grade", 1));
}
if (xueq != null && xueq.length() > 0) {
studentBean.setTerm(xueq);
}
if (shen != null && shen.length() > 0) {
studentBean.setParentIdentity(shen);
}
tv_name.setText(studentBean.getNickName());
tv_nian.setText(studentBean.getGrade() + studentBean.getTerm());
tv_sex.setText(studentBean.getGender() == 0 ? "男" : "女");
tv_shen.setText(studentBean.getParentIdentity());
if (!TextUtils.isEmpty(studentBean.getPhoto())) {
RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(this, 45));
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(this).load(studentBean.getPhoto()).apply(options).into(iv_avatar);
} else {
iv_avatar.setImageResource(studentBean.getGender() == 0 ? R.mipmap.ic_avatar_male : R.mipmap.ic_avatar_female);
}
if (!"虚拟".equals(studentBean.getTeacherIdentity())) {
tv_teacher.setText(studentBean.getTeacherName());
if (!TextUtils.isEmpty(studentBean.getTeacherAvatar())) {
RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(this, 25));
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(this).load(studentBean.getTeacherAvatar()).apply(options).into(iv_teacher);
} else {
iv_teacher.setImageResource(studentBean.getSex() == 0 ? R.mipmap.ic_teacher_male : R.mipmap.ic_teacher_female);
}
} else {
iv_teacher.setVisibility(View.GONE);
}
}
@Override
protected void initListener() {
view_2.setOnClickListener(view -> startActivity(NickActivity.class));
view_3.setOnClickListener(view -> startActivity(SexActivity.class));
view_4.setOnClickListener(view -> startActivity(NianActivity.class));
view_5.setOnClickListener(view -> startActivity(ShenActivity.class));
findViewById(R.id.tv_cancel).setOnClickListener(view -> onBackPressed());
btn_next.setOnClickListener(view -> {
editStudent();
});
}
public void editStudent() {
Map map = new HashMap();
map.put("nickName", studentBean.getNickName());
map.put("gender", studentBean.getGender());
map.put("gradeId", studentBean.getGradeId());
map.put("parentIdentity", studentBean.getParentIdentity());
map.put("term", studentBean.getTerm());
map.put("stuId", studentBean.getStuId());
NetWorks.editStudent(NetWorks.getMapRequestBody(map), new Observer<ResponseResult>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseResult responseResult) {
try {
boolean isSucceed = responseResult.getSuccess();
if (isSucceed) {
finish();
} else {
Toast.makeText(EditStudentActivity.this, responseResult.getMsg(), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
@Override
protected void initData() {
}
@Override
public void onNetChanged(int netWorkState) {
}
private void cropPhoto(Uri uri) {
Uri contentUri = Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg"));
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//Android 7.0需要临时添加读取Url的权限, 添加此属性是为了解决:调用裁剪框时候提示:图片无法加载或者加载图片失败或者无法加载此图片
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");//发送裁剪信号,去掉也能进行裁剪
intent.putExtra("scale", true);// 设置缩放
intent.putExtra("scaleUpIfNeeded", true);// 去黑边
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//上述两个属性控制裁剪框的缩放比例。
//当用户用手拉伸裁剪框时候,裁剪框会按照上述比例缩放。
intent.putExtra("outputX", 300);//属性控制裁剪完毕,保存的图片的大小格式。
intent.putExtra("outputY", 300);//你按照1:1的比例来裁剪的,如果最后成像是800*400,那么按照2:1的样式保存,
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//输出裁剪文件的格式
intent.putExtra("return-data", true);//是否返回裁剪后图片的Bitmap
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);//设置输出路径
startActivityForResult(intent, CROP_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//用相机返回的照片去调用剪裁也需要对Uri进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(this,
this.getPackageName() + ".fileprovider", tempFile);
cropPhoto(contentUri);
} else {
cropPhoto(Uri.fromFile(tempFile));
}
}
} else if (requestCode == ALBUM_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
cropPhoto(uri);
}
} else if (requestCode == CROP_REQUEST_CODE) {
if (intent == null) {
return;
}
Bundle bundle = intent.getExtras();
if (bundle != null) {
//在这里获得了剪裁后的Bitmap对象,可以用于上传
Bitmap image = bundle.getParcelable("data");
//设置到ImageView上
//也可以进行一些保存、压缩等操作后上传
File file = saveImage("head_output", image);
uploadAvatar(file);
// Log.d("dsadsadsa",path);
}
}
}
private void uploadAvatar(File file) {
NetWorks.uploadStudentAvatar(file, studentBean.getStuId(), new Observer<ResponseResult<Map<String, String>>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseResult<Map<String, String>> responseBody) {
if (responseBody.getData() != null && responseBody.getCode() == 200) {
String filePath = responseBody.getData().get("imageUrl");
RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(EditStudentActivity.this, 45));
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(EditStudentActivity.this).load(filePath).apply(options).into(iv_avatar);
}
}
@Override
public void onError(Throwable e) {
e.toString();
}
@Override
public void onComplete() {
}
});
}
public File saveImage(String name, Bitmap bmp) {
File appDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath());
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = name + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return file;
} catch (IOException e) {
Log.d("dsadsadsa", e.getLocalizedMessage());
e.printStackTrace();
}
return null;
}
private void getPicFromCamera() {
//用于保存调用相机拍照后所生成的文件
tempFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this,
this.getPackageName() + ".fileprovider", tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else { //否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
}
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
/**
* 从相册获取图片
*/
private void getPicFromAlbm() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
}
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferencesUtil.putData("setName", "");
SharedPreferencesUtil.putData("setSex", "");
SharedPreferencesUtil.putData("setNian", "");
SharedPreferencesUtil.putData("setShen", "");
SharedPreferencesUtil.putData("setXueq", "");
SharedPreferencesUtil.putData("grade", 0);
}
@OnClick(R.id.iv_avatar)
public void showButtonDialog(View view) {
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
View bottomView = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_layout, null);
dialog.setContentView(bottomView);
bottomView.findViewById(R.id.take_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
getPicFromCamera();
}
});
bottomView.findViewById(R.id.choose_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
getPicFromAlbm();
}
});
bottomView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
}