打开相机,打开相册,裁剪,上传

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IContract.IView {

private ImageView user_image;
private Button btn_commit;
private TextView user_name;
private Button pop_camera, pop_image, pop_cancel;
private PopupWindow popupWindow;
private PresenterImpl mPresenter;
private SharedPreferences sharedPreferences;

private String file = Environment.getExternalStorageDirectory() + "/iamge.png";
private SharedPreferences.Editor editor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
}

private void initView() {
    user_image = (ImageView) findViewById(R.id.user_image);
    btn_commit = (Button) findViewById(R.id.btn_commit);
    user_name = findViewById(R.id.user_name);
    mPresenter = new PresenterImpl(this);

    btn_commit.setOnClickListener(this);
    user_image.setOnClickListener(this);

    //创建popWindow 自定义布局
    View view = View.inflate(this, R.layout.pop_view, null);

    pop_camera = view.findViewById(R.id.pop_camera);
    pop_image = view.findViewById(R.id.pop_image);
    pop_cancel = view.findViewById(R.id.pop_cancel);
    /*
     *  第一个参数: 布局 视图
     *  第二个参数: 宽度
     *  第三个参数: 高度
     *  第四个参数:使popupWindow 点击空白处也可以消失
     * */
    popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
    //设置背景颜色
    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.GRAY));

    //初始化Sp
    sharedPreferences = getSharedPreferences("nc", Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_commit:
            //点击提交,上传头像到接口
            mPresenter.startRequest(new File(file));
            break;
        case R.id.user_image:
            //点击弹出popwindow,在底部显示
            popupWindow.showAtLocation(View.inflate(MainActivity.this, R.layout.pop_view, null)
                    , Gravity.BOTTOM, 0, 0);
            break;
    }
}

public void initData() {
    pop_camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //打开相机
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(file)));
            startActivityForResult(intent, 100);
            //关闭popupwindow
            popupWindow.dismiss();
        }
    });
    pop_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent it1 = new Intent(Intent.ACTION_PICK);
            it1.setType("image/*");
            startActivityForResult(it1, 200);

            //关闭popupwindow
            popupWindow.dismiss();
        }
    });
    pop_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //关闭popupwindow
            popupWindow.dismiss();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK) {
        Uri uri = Uri.fromFile(new File(file));
        Crop(uri);
    } else if (requestCode == 200 && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        Crop(uri);
    } else if (requestCode == 300 && resultCode == RESULT_OK) {

        Bitmap bitmap = data.getParcelableExtra("data");
        user_image.setImageBitmap(bitmap);
    }
}

//裁剪
public void Crop(Uri uri) {
    Intent it1 = new Intent("com.android.camera.action.CROP");
    it1.setDataAndType(uri, "image/*");
    //支持裁剪
    it1.putExtra("crop", true);

    it1.putExtra("aspectX", 250);
    it1.putExtra("aspectY", 250);
    it1.putExtra("outputX", 1);
    it1.putExtra("outputY", 1);
    it1.putExtra("return-data", true);
    startActivityForResult(it1, 300);
}

@Override
public void onSuccess(String success) {
    Gson gson = new Gson();
    Results results = gson.fromJson(success, Results.class);

    user_name.setText(results.getHeadPath());
    //存入sp
    editor.putString("headPath", results.getHeadPath());
    editor.commit();
}

@Override
public void onError(String error) {
    Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPresenter != null) {
        mPresenter.onDeattch();
    }
}

工具类

MediaType mediaType = MediaType.parse(“multipart/form-data;charset=utf-8”);
RequestBody body = RequestBody.create(mediaType, file);
MultipartBody.Part part = MultipartBody.Part.createFormData(“image”, file.getName(), body);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值