首页 > 解决方案 > 我想裁剪图像以设置配置文件

问题描述

我是新来的,正在学习android开发。我想裁剪图像以设置个人资料图片。当我使用下面的代码时,它没有完成它的跳转来捕捉(出了点问题)。请帮我解决这个问题。在 xml 上使用图像视图并在 mainfest 上获得读写权限。并使用 ArthurHub/Android-Image-Cropper 进行裁剪功能

enter code here  select_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            premission();

            if (pre == 2) {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                Log.d("pp", "pp");
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            }
        }
    });

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
      { super.onActivityResult(requestCode, resultCode, data);

    try
    {
        if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null != data)
        {

            CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1, 1)
                    .start(this);
            Uri selectedImage = data.getData();
        }



        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if (resultCode == RESULT_OK)
            {
                 Uri selectedImage = result.getUri();


                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();


                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                profileimage.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
                Drawable myDrawable = profileimage.getDrawable();
                profileimage.buildDrawingCache();
                Bitmap bmap = profileimage.getDrawingCache();
                BitmapDrawable bitmapDrawable = ((BitmapDrawable) myDrawable);
                Bitmap bitmap1 = bitmapDrawable.getBitmap();
                final ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap1.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                imageInByte = stream.toByteArray();
                encoded = Base64.encodeToString(imageInByte, Base64.DEFAULT);



            } else
                {

                Toast.makeText(this, "You have not picked Image",
                        Toast.LENGTH_LONG).show();
                }
        }

    }
    catch (Exception e)
    {

        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();

    }
}

标签: javaandroid

解决方案


推荐阅读