图片裁剪

代码如下:
package com.ztxx.customer.activity.four;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.handmark.pulltorefresh.library.internal.Utils;
import com.ztxx.customer.BaseActivity;
import com.ztxx.customer.R;
import com.ztxx.customer.activity.MainActivity;
import com.ztxx.customer.dialog.DialogTakePhoto;
import com.ztxx.customer.dialog.LoadingProgress;
import com.ztxx.customer.http.HttpUtil;
import com.ztxx.customer.http.MyCallBack;
import com.ztxx.customer.http.RMParams;
import com.ztxx.customer.http.UrlConst;
import com.ztxx.customer.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;

import java.io.File;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 修改头像
 */
public class ModifyPhotoActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_modify_photo;
    private RadioButton rb_nv;
    private RadioButton rb_nan;
    private RadioGroup rg_xingbie;
    private EditText et_modify_nicheng;
    private Uri uritempFile;
    private String sex="2";
    private String nicheng ;
    private String logo;
    private String tag; //登录:actlogin        four:frgfour
    private ImageView iv_back;
    private TextView tv_baocun;

    @Override
    public void initViews() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.act_modify_photo, null);
        setframlayout(view);
        setTitleGone();
        iv_back = (ImageView) view.findViewById(R.id.iv_back);  //返回
        tv_baocun = (TextView) view.findViewById(R.id.tv_right);//保存
        iv_modify_photo = (ImageView) view.findViewById(R.id.iv_modify_photo);
        rb_nv = (RadioButton) view.findViewById(R.id.rb_nv);
        rb_nan = (RadioButton) view.findViewById(R.id.rb_nan);
        rg_xingbie = (RadioGroup) view.findViewById(R.id.rg_xingbie);
        et_modify_nicheng = (EditText) view.findViewById(R.id.et_modify_nicheng);
        iv_modify_photo.setOnClickListener(this);
        nicheng = getIntent().getStringExtra("nicehng");
        logo = getIntent().getStringExtra("logo");
        tag = getIntent().getStringExtra("tag");

        //展示头像
        Glide.with(mContext)
                .load(logo)
                .asBitmap()
                .centerCrop()
                .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                .into(new BitmapImageViewTarget(iv_modify_photo) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                iv_modify_photo.setImageDrawable(circularBitmapDrawable);
            }
        });

        /**
         * 昵称只可修改一次
         */
        if (nicheng.isEmpty()){
            et_modify_nicheng.setEnabled(true);
            et_modify_nicheng.setFocusable(true);

        }else {
            et_modify_nicheng.setEnabled(false);
            et_modify_nicheng.setFocusable(false);
        }
        et_modify_nicheng.setText(nicheng);
    }

    @Override
    public void initListeners() {

        //保存
        tv_baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!et_modify_nicheng.getText().toString().trim().equals("")){
                    XiuGai();
                }else {
                    ToastUtils.shortToast(mContext,R.string.qingwanzhengxinxi);
                }
            }
        });
        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tag.equals("actlogin")){
                    if (!et_modify_nicheng.getText().toString().trim().equals("")){
                        toActivity(MainActivity.class);
                    }
                }else {
                    finish();
                }
            }
        });

    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击添加照片修改头像
            case R.id.iv_modify_photo:
                DialogTakePhoto photodialog = new DialogTakePhoto(mContext, new DialogTakePhoto.OnDialoglister() {
                    @Override
                    public void onTuku() {
                        // �����ѡȡ
                        if (Build.VERSION.SDK_INT < 19) {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(intent, 102);
                        } else {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_PICK);
                            startActivityForResult(intent, 102);
                        }
                    }

                    @Override
                    public void onPaizhao() {
                        // ����
                        Uri imageUri = null;
                        String fileName = null;
                        SharedPreferences sharedPreferences = getSharedPreferences("temp", Context.MODE_WORLD_WRITEABLE);
                        fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("tempName", fileName);
                        editor.commit();
                        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), fileName));
                        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        LogUtil.i("456" + imageUri);
                        startActivityForResult(openCameraIntent, 102);
                    }
                });
                photodialog.show();

                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 102:
                if (resultCode != RESULT_OK) {
                    return;
                }
                Uri uri = null;

                if (data != null) {
                    uri = data.getData();
                } else {
                    String fileName = getSharedPreferences("temp", Context.MODE_WORLD_WRITEABLE).getString("tempName", "");
                    uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), fileName));
                }
                String thePath = Utils.getPath(this, uri);
                cropImage(Uri.fromFile(new File(thePath)), 500, 500, 100);
                break;
            case 100:
                if (resultCode != RESULT_OK) {
                    return;
                }
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uritempFile));
                    iv_modify_photo.setImageBitmap(bitmap);
                    /**
                     * uritempFile.getPath(),上传头像所需要的路径
                     */
                  //上传头像
                  UpPhoto();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }
    /**
     * 上传头像
     */
    public void UpPhoto() {
        String path=Utils.getPath(mContext,uritempFile);
        Map<String, Object> stringObjectMap = new RMParams(mContext).UP_Photo();

        HttpUtil.uploadSinglePhoto(UrlConst.uploadaPic,path , stringObjectMap, new Callback.ProgressCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject j = new JSONObject(result);
                    boolean sussess = j.getBoolean("success");
                    if (sussess == true) {
                        logo = j.getString("message");
                        ToastUtils.shortToast(mContext,R.string.shangchuantupianchenggong);
                        //刷新我的资料页面
                        EventBus.getDefault().post("112");
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(CancelledException arg0) {
             //   LogUtils.i("123", "onCancelled");
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
               // LogUtils.i("123", "onError");
            }

            @Override
            public void onFinished() {
               // LogUtils.i("123", "onFinished");
            }

            @Override
            public void onLoading(long arg0, long arg1, boolean arg2) {
               // LogUtils.i("123", "onLoading" + "---" + arg0 + "---" + arg1 + "---" + arg2);
            }

            @Override
            public void onStarted() {
               // LogUtils.i("123", "onStarted");
            }

            @Override
            public void onWaiting() {
               // LogUtils.i("123", "onWaiting");
            }
        });
    }

    /截取图片//

    /**
     * ��ȡͼƬ
     *
     * @param uri
     * @param outputX
     * @param outputY
     * @param requestCode
     */
    public void cropImage(Uri uri, int outputX, int outputY, int requestCode) {
        Intent innerIntent = new Intent("com.android.camera.action.CROP");
        innerIntent.setDataAndType(uri, "image/*");
        innerIntent.putExtra("crop", "true");// ���ܳ�������С���򣬲�Ȼû�м������ܣ�ֻ��ѡȡͼƬ
        innerIntent.putExtra("aspectX", 1); // �Ŵ���С������X
        innerIntent.putExtra("aspectY", 1);// �Ŵ���С������X ����ı���Ϊ�� 1:1
        innerIntent.putExtra("outputX", 320); // ������������ͼƬ��С
        innerIntent.putExtra("outputY", 320);
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Haoxiangni/Temp/");
        if (!file.exists()) {
            file.mkdirs();
        }
        uritempFile = Uri.parse("file:///" + new File(file, "temp.jpg").getAbsolutePath());
        innerIntent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
        innerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        innerIntent.putExtra("noFaceDetection", true);
        innerIntent.putExtra("return-data", false);
        startActivityForResult(innerIntent, requestCode);
    }

    public void XiuGai() {
            nicheng = et_modify_nicheng.getText().toString();
            HttpUtil.Post2(UrlConst.XiuGaiInfo, new RMParams(mContext).XiuGaiInfo(sex,"",nicheng , ""), new MyCallBack<String>() {
                private LoadingProgress progress;
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    try {
                        JSONObject j = new JSONObject(result);
                        boolean success = j.getBoolean("success");
                        String message = j.getString("message");
                        if (success == true) {
                            ToastUtils.shortToast(mContext, R.string.xiugaichenggong);
                            //刷新我的资料页面
                            EventBus.getDefault().post("112");
                            if (tag.equals("actlogin")){
                                toActivity(MainActivity.class);
                            }else {
                                finish();
                            }

                        } else {
                            ToastUtils.shortToast(mContext, message);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onStarted() {
                    super.onStarted();
                    progress = new LoadingProgress(mContext);
                    progress.showDialog(getResources().getString(R.string.zhengzaijiazai));
                }
                @Override
                public void onFinished() {
                    super.onFinished();
                    progress.dismissDialog();
                }
            });
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值