|
@@ -0,0 +1,291 @@
|
|
|
+package com.ynstkz.shitu.android.activity;
|
|
|
+
|
|
|
+import android.app.ProgressDialog;
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
+import android.provider.MediaStore;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.bumptech.glide.Glide;
|
|
|
+import com.common.library.okhttp.callback.Callback;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.ynstkz.shitu.android.R;
|
|
|
+import com.ynstkz.shitu.android.base.BaseBean;
|
|
|
+import com.ynstkz.shitu.android.base.TitleBarActivity;
|
|
|
+import com.ynstkz.shitu.android.data.RequestGroup;
|
|
|
+import com.ynstkz.shitu.android.utils.FileUtils;
|
|
|
+import com.ynstkz.shitu.android.utils.ImageUtils;
|
|
|
+import com.ynstkz.shitu.android.view.SelectPicDialog;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+import butterknife.Bind;
|
|
|
+import okhttp3.Call;
|
|
|
+import okhttp3.Response;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 作者:fuchangle on 2018/3/18 20:18
|
|
|
+ */
|
|
|
+
|
|
|
+public class AlterHeadpicActivity extends TitleBarActivity implements View.OnClickListener {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求相册
|
|
|
+ */
|
|
|
+ public static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 0;
|
|
|
+ /**
|
|
|
+ * 请求相机
|
|
|
+ */
|
|
|
+ public static final int REQUEST_CODE_GETIMAGE_BYCAMERA = 1;
|
|
|
+ /**
|
|
|
+ * 请求裁剪
|
|
|
+ */
|
|
|
+ public static final int REQUEST_CODE_GETIMAGE_BYCROP = 2;
|
|
|
+
|
|
|
+ @Bind(R.id.iv_headPic)
|
|
|
+ ImageView ivHeadPic;
|
|
|
+ @Bind(R.id.tv_title)
|
|
|
+ TextView tvTitle;
|
|
|
+
|
|
|
+ private SelectPicDialog selectPicDialog;
|
|
|
+ private ProgressDialog progressDialog;
|
|
|
+
|
|
|
+ private String protraitPath;
|
|
|
+ private File protraitFile;
|
|
|
+ private Uri origUri;
|
|
|
+ private Uri cropUri;
|
|
|
+ private final static int CROP = 200;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ initView();
|
|
|
+ initData();
|
|
|
+ setListener();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int getLayoutId() {
|
|
|
+ return R.layout.activity_alter_headpic;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
+ tvTitle.setText("个人头像");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void initData() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setListener() {
|
|
|
+
|
|
|
+ ivHeadPic.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ if (selectPicDialog == null) {
|
|
|
+ selectPicDialog = new SelectPicDialog(AlterHeadpicActivity.this, AlterHeadpicActivity.this);
|
|
|
+ }
|
|
|
+ selectPicDialog.show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ switch (view.getId()) {
|
|
|
+ case R.id.take_photo_layout:
|
|
|
+ closePopDialog();
|
|
|
+ startActionCamera();
|
|
|
+ break;
|
|
|
+ case R.id.choose_from_gallery_layout:
|
|
|
+ closePopDialog();
|
|
|
+ startImagePick();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void closePopDialog() {
|
|
|
+ if (selectPicDialog != null && selectPicDialog.isShowing()) {
|
|
|
+ selectPicDialog.dismiss();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 相机拍照
|
|
|
+ */
|
|
|
+ private void startActionCamera() {
|
|
|
+ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
+ intent.putExtra(MediaStore.EXTRA_OUTPUT, getCameraTempFile());
|
|
|
+ startActivityForResult(intent, REQUEST_CODE_GETIMAGE_BYCAMERA);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拍照保存的绝对路径
|
|
|
+ */
|
|
|
+ private Uri getCameraTempFile() {
|
|
|
+ String storageState = Environment.getExternalStorageState();
|
|
|
+ if (storageState.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
+ File savedir = new File(getFileSavePath());
|
|
|
+ if (!savedir.exists()) {
|
|
|
+ savedir.mkdirs();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ showToast("无法保存上传的头像,请检查SD卡是否挂载");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
|
|
|
+ .format(new Date());
|
|
|
+ // 照片命名
|
|
|
+ String cropFileName = "st_camera_" + timeStamp + ".jpg";
|
|
|
+ // 裁剪头像的绝对路径
|
|
|
+ protraitPath = getFileSavePath() + cropFileName;
|
|
|
+ protraitFile = new File(protraitPath);
|
|
|
+ cropUri = Uri.fromFile(protraitFile);
|
|
|
+ this.origUri = this.cropUri;
|
|
|
+ return this.cropUri;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件保存路径
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getFileSavePath() {
|
|
|
+ return Environment
|
|
|
+ .getExternalStorageDirectory().getAbsolutePath()
|
|
|
+ + "/shitu/Portrait/";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拍照后裁剪
|
|
|
+ *
|
|
|
+ * @param data 原始图片
|
|
|
+ * 裁剪后图片
|
|
|
+ */
|
|
|
+ private void startActionCrop(Uri data) {
|
|
|
+ Intent intent = new Intent("com.android.camera.action.CROP");
|
|
|
+ intent.setDataAndType(data, "image/*");
|
|
|
+ intent.putExtra("output", this.getUploadTempFile(data));
|
|
|
+ intent.putExtra("crop", "true");
|
|
|
+ intent.putExtra("aspectX", 1);// 裁剪框比例
|
|
|
+ intent.putExtra("aspectY", 1);
|
|
|
+ intent.putExtra("outputX", CROP);// 输出图片大小
|
|
|
+ intent.putExtra("outputY", CROP);
|
|
|
+ intent.putExtra("scale", true);// 去黑边
|
|
|
+ intent.putExtra("scaleUpIfNeeded", true);// 去黑边
|
|
|
+ startActivityForResult(intent,
|
|
|
+ REQUEST_CODE_GETIMAGE_BYSDCARD);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 裁剪头像的绝对路径
|
|
|
+ private Uri getUploadTempFile(Uri uri) {
|
|
|
+ String storageState = Environment.getExternalStorageState();
|
|
|
+ if (storageState.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
+ File savedir = new File(getFileSavePath());
|
|
|
+ if (!savedir.exists()) {
|
|
|
+ savedir.mkdirs();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ showToast("无法保存上传的头像,请检查SD卡是否挂载");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
|
|
|
+ .format(new Date());
|
|
|
+ String thePath = ImageUtils.getAbsolutePathFromNoStandardUri(uri);
|
|
|
+
|
|
|
+ // 如果是标准Uri
|
|
|
+ if (TextUtils.isEmpty(thePath)) {
|
|
|
+ thePath = ImageUtils.getAbsoluteImagePath(this, uri);
|
|
|
+ }
|
|
|
+ String ext = FileUtils.getFileFormat(thePath);
|
|
|
+ ext = TextUtils.isEmpty(ext) ? "jpg" : ext;
|
|
|
+ // 照片命名
|
|
|
+ String cropFileName = "st_crop_" + timeStamp + "." + ext;
|
|
|
+ // 裁剪头像的绝对路径
|
|
|
+ protraitPath = getFileSavePath() + cropFileName;
|
|
|
+ protraitFile = new File(protraitPath);
|
|
|
+
|
|
|
+ cropUri = Uri.fromFile(protraitFile);
|
|
|
+ return this.cropUri;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 选择图片裁剪
|
|
|
+ */
|
|
|
+ private void startImagePick() {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
+ intent.setType("image/*");
|
|
|
+ startActivityForResult(Intent.createChooser(intent, "选择图片"), REQUEST_CODE_GETIMAGE_BYCROP);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传头像
|
|
|
+ */
|
|
|
+ private void userResetHeadpic() {
|
|
|
+
|
|
|
+ if (!TextUtils.isEmpty(protraitPath) && protraitFile.exists()) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ showToast("图像不存在,上传失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ progressDialog = new ProgressDialog(AlterHeadpicActivity.this);
|
|
|
+ progressDialog.setMessage("正在提交...");
|
|
|
+ progressDialog.show();
|
|
|
+
|
|
|
+ RequestGroup.userResetHeadpic(protraitFile, new Callback() {
|
|
|
+ @Override
|
|
|
+ public Object parseNetworkResponse(Response response, int id) throws Exception {
|
|
|
+ return new Gson().fromJson(response.body().string(), BaseBean.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Call call, Exception e, int id) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ showToast(getString(R.string.error_msg));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Object response, int id) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ BaseBean baseBean = (BaseBean) response;
|
|
|
+ if (baseBean != null) {
|
|
|
+ showToast(baseBean.getMsg());
|
|
|
+ if ("200".equals(baseBean.getCode())) {
|
|
|
+ Glide.with(AlterHeadpicActivity.this).load(protraitFile).into(ivHeadPic);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ switch (requestCode) {
|
|
|
+ case REQUEST_CODE_GETIMAGE_BYCAMERA:
|
|
|
+ startActionCrop(origUri);// 拍照后裁剪
|
|
|
+ case REQUEST_CODE_GETIMAGE_BYCROP:
|
|
|
+ if (data != null) {
|
|
|
+ startActionCrop(data.getData());// 选图后裁剪
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case REQUEST_CODE_GETIMAGE_BYSDCARD:
|
|
|
+ userResetHeadpic();// 上传新照片
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|