touxiang

private void showChoosePicDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("设置头像");
    String[] items = { "选择本地照片", "拍照" };
    builder.setNegativeButton("取消", null);
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case CHOOSE_PICTURE: // 选择本地照片
                   /* Intent openAlbumIntent = new Intent(
                            Intent.ACTION_GET_CONTENT);
                    openAlbumIntent.setType("image*//**//*");
                    startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);*/
                    Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                    intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent1, CHOOSE_PICTURE);

                    break;
                case TAKE_PICTURE: // 拍照
                    Intent openCameraIntent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    tempUri = Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory(), "image.jpg"));
                    // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
                    startActivityForResult(openCameraIntent, TAKE_PICTURE);
                    break;
            }
        }
    });
    builder.create().show();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) { // 如果返回码是可以用的
        switch (requestCode) {
            case TAKE_PICTURE:
                startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
                break;
            case CHOOSE_PICTURE:
                startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
                break;
            case CROP_SMALL_PICTURE:
                if (data != null) {
                    setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
                }
                break;
        }
    }
}

protected void setImageToView(Intent data) {
    Bundle extras = data.getExtras();
    if (extras != null) {
        try {
            Bitmap photo = extras.getParcelable("data");
            iv_geren_touxiang.setImageBitmap(photo);
            File file=new File("mnt/sdcard/yang.png");
            if(!file.exists()){
                try {

                    file.createNewFile();

                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            photo.compress(Bitmap.CompressFormat.PNG,100,outputStream);


            if(file!=null) {
                int uid = sp.getInt("uid", 0);

                String filename = file.getName();
                Map<String, Object> params = new HashMap<>();
                params.put("uid", uid);
                OkHttpClient okHttpClient = new OkHttpClient();
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
                builder.addFormDataPart("file", filename, requestBody);
                if (params != null) {
                    // map 里面是请求中所需要的 key 和 value
                    for (Map.Entry entry : params.entrySet()) {

                        builder.addFormDataPart(valueOf(entry.getKey()),valueOf(entry.getValue()));

                    }
                }

                Request request = new Request.Builder().url("http://120.27.23.105/file/upload").post(builder.build()).build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String string = response.body().string();
                        System.out.println("=========" +string);
                        Gson gson = new Gson();
                        Yong yong = gson.fromJson(string, Yong.class);
                        Yong.DataBean data1 = yong.data;


                    }
                });
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}



private void startPhotoZoom(Uri uri) {

    if (uri == null) {

        Log.i("tag", "The uri is not exist.");
    }
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 150);
    intent.putExtra("outputY", 150);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, CROP_SMALL_PICTURE);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值