() {
171 | @Override
172 | public void accept(View o) throws Exception {
173 | String name = mUserName.getText().toString().trim();
174 | if (TextUtils.isEmpty(name)) {
175 | name = mUser.getNickname();
176 | }
177 | mUser.setNickname(name);
178 |
179 | mLoadingDialog = KSVSDialodManager.showLoadingDialog(SettingActivity.this, null);
180 |
181 | if (mDstFile == null || !mDstFile.exists()) {
182 | setUserInfo(null);
183 | } else {
184 | mKitManager.uploadFile(mUser.getUid(), mDstFile.getPath(),
185 | new IKSVSShortVideoListener() {
186 | @Override
187 | public void onInfo(String type, Bundle data) {
188 | if (type.equals(IKSVSShortVideoListener.KSVS_LISTENER_TYPE_UPLOAD)) {
189 | int status = data.getInt(IKSVSShortVideoListener.KSVS_LISTENER_BUNDLE_STATUS_INT);
190 | if (status == IKSVSShortVideoUpload.UPLOAD_INFO_COMPLETE) {
191 | String path = data.getString(IKSVSShortVideoUpload.UPLOAD_INFO_FILE_PATH);
192 | KLog.d(TAG, "获取到的path是:" + path);
193 | mUser.setHeadUrl(path);
194 | //图片上传成功
195 | setUserInfo(path);
196 | }
197 | }
198 | }
199 |
200 | @Override
201 | public void onError(String type, int error, Bundle data) {
202 | KLog.e(TAG, "个人信息修改失败,error=" + error);
203 | ToastUtils.showToast(SettingActivity.this, R.string.setting_setuser_error);
204 | if (mLoadingDialog != null)
205 | mLoadingDialog.dismiss();
206 | }
207 |
208 | @Override
209 | public void onProgress(String type, int params, int progress) {
210 |
211 | }
212 | });
213 |
214 | }
215 | }
216 | };
217 |
218 | @Override
219 | protected void onDestroy() {
220 | super.onDestroy();
221 | if (mRequestManager != null) {
222 | mRequestManager.releaseAllReuqest();
223 | }
224 | mRequestManager = null;
225 | deleteFile();
226 | }
227 |
228 | /**
229 | * 拍一张效果
230 | * 6.0以上手机需要动态请求权限
231 | */
232 | private void openCamera() {
233 | int cameraPerm = ActivityCompat.checkSelfPermission(SettingActivity.this,
234 | Manifest.permission.CAMERA);
235 | int writePerm = ActivityCompat.checkSelfPermission(SettingActivity.this,
236 | Manifest.permission.WRITE_EXTERNAL_STORAGE);
237 |
238 | if (cameraPerm != PackageManager.PERMISSION_GRANTED ||
239 | writePerm != PackageManager.PERMISSION_GRANTED) {
240 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
241 | ToastUtils.showToast(SettingActivity.this, R.string.setting_no_camera_permissions);
242 | KLog.e(TAG, "No Camera permission,check permission");
243 | } else {
244 | String[] permissions = {Manifest.permission.CAMERA,
245 | Manifest.permission.WRITE_EXTERNAL_STORAGE,
246 | Manifest.permission.READ_EXTERNAL_STORAGE,};
247 | ActivityCompat.requestPermissions(SettingActivity.this, permissions,
248 | PERMISSION_REQUEST_CAMERA);
249 | }
250 | } else {
251 | openImageCapture();
252 | }
253 | }
254 |
255 | /**
256 | * 开启照相功能,文件保存在临时目录下。名称为user_logo
257 | *
258 | * 注意,在7.0及以上的设备,因为权限的问题,所以不能使用URi,必须使用ContentValues包装以下
259 | */
260 | private void openImageCapture() {
261 | mSrcFile = new File(Utils.getTempLocalVideoPath(SettingActivity.this),
262 | "user_logo.png");
263 | if (mSrcFile.exists()) {
264 | mSrcFile.delete();
265 | }
266 | Intent intent = new Intent();
267 | intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
268 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
269 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mSrcFile));
270 | } else {
271 | Uri imgUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider",
272 | mSrcFile);
273 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
274 | }
275 | startActivityForResult(intent, TAKE_PHOTO);
276 | }
277 |
278 |
279 | @Override
280 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
281 | @NonNull int[] grantResults) {
282 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
283 | switch (requestCode) {
284 | case PERMISSION_REQUEST_CAMERA: {
285 | if (grantResults.length >= 2 &&
286 | grantResults[0] == PackageManager.PERMISSION_GRANTED &&
287 | grantResults[1] == PackageManager.PERMISSION_GRANTED) {
288 | openImageCapture();
289 | } else {
290 | ToastUtils.showToast(SettingActivity.this, R.string.setting_no_camera_permissions);
291 | }
292 | }
293 | break;
294 | case PERMISSION_REQUEST_WRITE_EXTERNL:
295 | if (grantResults.length > 0 &&
296 | grantResults[0] == PackageManager.PERMISSION_GRANTED) {
297 | openImageGallery();
298 | } else {
299 | ToastUtils.showToast(SettingActivity.this, R.string.setting_no_write_permissions);
300 | }
301 | break;
302 | }
303 | }
304 |
305 | /**
306 | * 打开图库
307 | */
308 | private void openGallery() {
309 | int writePerm = ActivityCompat.checkSelfPermission(SettingActivity.this,
310 | Manifest.permission.WRITE_EXTERNAL_STORAGE);
311 |
312 | if (writePerm != PackageManager.PERMISSION_GRANTED) {
313 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
314 | ToastUtils.showToast(SettingActivity.this, R.string.setting_no_write_permissions);
315 | KLog.e(TAG, "No write permission,check permission");
316 | } else {
317 | String[] permissions = {
318 | Manifest.permission.WRITE_EXTERNAL_STORAGE,
319 | Manifest.permission.READ_EXTERNAL_STORAGE,};
320 | ActivityCompat.requestPermissions(SettingActivity.this, permissions,
321 | PERMISSION_REQUEST_WRITE_EXTERNL);
322 | }
323 | } else {
324 | openImageGallery();
325 | }
326 |
327 | }
328 |
329 | private void openImageGallery() {
330 |
331 | Intent intent = new Intent(Intent.ACTION_PICK, null);
332 | intent.setDataAndType(
333 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
334 | "image/*");
335 | startActivityForResult(intent, CHOOSE_PHOTO);
336 | }
337 |
338 | /**
339 | * 图片剪裁
340 | */
341 | private void cutPhoto(Uri srcUri) {
342 | Intent intent = new Intent("com.android.camera.action.CROP");
343 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
344 | intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
345 | intent.setDataAndType(srcUri, "image/*");
346 | intent.putExtra("scale", true);
347 | intent.putExtra("aspectX", 1);
348 | intent.putExtra("aspectY", 1);
349 | intent.putExtra("outputX", 100);
350 | intent.putExtra("outputY", 100);
351 | intent.putExtra("noFaceDetection", true);
352 | intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
353 | intent.putExtra("return-data", false);
354 | mDstFile = new File(Utils.getTempLocalVideoPath(SettingActivity.this),
355 | "user_logo_dst.png");
356 | if (mDstFile.exists()) {
357 | mDstFile.delete();
358 | }
359 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mDstFile));
360 | try {
361 | startActivityForResult(intent, CUT_PHOTO);// 启动裁剪程序
362 | } catch (Exception e) {
363 | ToastUtils.showToast(SettingActivity.this, R.string.setting_no_picture);
364 | }
365 | }
366 |
367 | private void deleteFile() {
368 | File[] files = new File[2];
369 | files[0] = mDstFile != null ? mDstFile : new File("");
370 | files[1] = mSrcFile != null ? mSrcFile : new File("");
371 | Observable.fromArray(files)
372 | .observeOn(Schedulers.io())
373 | .subscribe(new Consumer() {
374 | @Override
375 | public void accept(File file) throws Exception {
376 | if (file != null && file.exists()) {
377 | file.delete();
378 | }
379 | }
380 | });
381 | }
382 |
383 | @Override
384 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
385 | super.onActivityResult(requestCode, resultCode, data);
386 | if (resultCode != RESULT_OK) {
387 | return;
388 | }
389 | switch (requestCode) {
390 | case TAKE_PHOTO:
391 | Uri imgUri;
392 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
393 | imgUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider",
394 | mSrcFile);
395 | } else {
396 | imgUri = Uri.fromFile(mSrcFile);
397 | }
398 | cutPhoto(imgUri);
399 | break;
400 | case CHOOSE_PHOTO:
401 | cutPhoto(data.getData());
402 | break;
403 | case CUT_PHOTO:
404 | if (mDstFile != null && mDstFile.exists()) {
405 | Glide.with(SettingActivity.this)
406 | .load(mDstFile)
407 | .placeholder(R.drawable.user_icon)
408 | .error(R.drawable.user_icon)
409 | .skipMemoryCache(true)
410 | .dontAnimate()
411 | .diskCacheStrategy(DiskCacheStrategy.NONE)
412 | .transform(mCircleTransform)
413 | .into(mUserIcon);
414 | }
415 | break;
416 | }
417 | }
418 |
419 | private Consumer mSexSelectClick = new Consumer() {
420 | @Override
421 | public void accept(View o) throws Exception {
422 | int type = (int) o.getTag();
423 | switch (type) {
424 | case SelectWindow.ITEM1_CLICK:
425 | mSexSelect.setText("男");
426 | mUser.setGender(false);
427 | break;
428 | case SelectWindow.ITEM2_CLICK:
429 | mSexSelect.setText("女");
430 | mUser.setGender(true);
431 | break;
432 | }
433 | }
434 | };
435 |
436 |
437 | private Consumer mUserIconSelectClick = new Consumer() {
438 | @Override
439 | public void accept(View o) throws Exception {
440 | int type = (int) o.getTag();
441 | switch (type) {
442 | case SelectWindow.ITEM1_CLICK:
443 | openCamera();
444 | break;
445 | case SelectWindow.ITEM2_CLICK:
446 | openGallery();
447 | break;
448 | }
449 | }
450 | };
451 |
452 |
453 | private DefaultOnClick mOnClick = new DefaultOnClick(null, new Consumer() {
454 | @Override
455 | public void accept(View view) throws Exception {
456 | InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
457 | imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
458 | if (view.getId() == R.id.fl_user_icon_select) {// 头像选择
459 | if (mSelectWindow != null) {
460 | mSelectWindow.dismissWindow();
461 | }
462 | mSelectWindow = new SelectWindow(SettingActivity.this);
463 | mSelectWindow.setViewOnClick(mUserIconSelectClick);
464 | mSelectWindow.showWindow(findViewById(R.id.ll_root_view));
465 | } else if (view.getId() == R.id.tv_sex_select) { //性别选择
466 | if (mSelectWindow != null) {
467 | mSelectWindow.dismissWindow();
468 | }
469 | mSelectWindow = new SelectWindow(SettingActivity.this, "男", "女");
470 | mSelectWindow.setViewOnClick(mSexSelectClick);
471 | mSelectWindow.showWindow(findViewById(R.id.ll_root_view));
472 | } else if (view.getId() == R.id.tv_logout) { //退出登录
473 | KSVSDialodManager.showMessageDialog(
474 | SettingActivity.this,
475 | R.string.setting_logout_message,
476 | R.string.setting_logout_confirm,
477 | new Consumer