获取输出裁剪的图像URI

时间:2017-12-13 11:51:59

标签: android image android-intent crop

我已经在我的应用程序中使用android intent实现了图像裁剪,如下所示:

CropIntent = new Intent("com.android.camera.action.CROP");

我收到了裁剪后的图片并显示在我的用户界面上但是我想要执行更多用例,例如上传裁剪后的图片uri。问题是活动中没有返回uri。

这是我的裁剪图片意图摘要:

galleryFAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "Gallery Btn Clicked");
            selectFromGallery();
        }
    });

private void selectFromGallery() {

    galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(Intent.createChooser(galleryIntent, "Choose From"), 2);
}

活动结果方法中的代码:

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

    InputStream inputStream;

    if (requestCode == 2 && resultCode == RESULT_OK){
        if (data != null){
            cropImageUri = data.getData();
            userImage.setImageURI(cropImageUri);
            try {
                originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), cropImageUri);

                inputStream = getContentResolver().openInputStream(data.getData());
                originalBitmap = BitmapFactory.decodeStream(inputStream);

                Log.d(TAG, "Original bitmap byte count is:\t" + originalBitmap.getByteCount());

                resizedBitmap = getResizedBitmap(originalBitmap, 200);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Log.d(TAG, "Resized bitmap byte count is:\t" + resizedBitmap.getByteCount());

                try {
                    File file = saveBitmap("avatar_image");
                    upLoadFile(file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }


                byte[] bytes = stream.toByteArray();

                /**
                 *   For Shared Preferences
                 * **/

                encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);

                imagePrefs = getSharedPreferences("ImagePrefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = imagePrefs.edit();
                editor.putString("image_user", encodedImage).commit();

                upLoadBytes(bytes);

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

            cropImage();
        }

    } else if (requestCode == PERM_CODE){
        if (data != null){

            //I want to get the cropped image uri here but unable to. Have tried converting the bitmap rcvd here to uri but in activity, ntn is returned. kindly guide me

            Bundle bundle = data.getExtras();
            originalBitmap = bundle.getParcelable("data");
            userImage.setImageBitmap(resizedBitmap);
        }

    }

}

我的裁剪图片功能:

private void cropImage() {
    try {
        CropIntent = new Intent("com.android.camera.action.CROP");

        CropIntent.setDataAndType(cropImageUri, "image/*");
        CropIntent.putExtra("crop", true);
        CropIntent.putExtra("outputX", 200);
        CropIntent.putExtra("outputY", 200);
        CropIntent.putExtra("aspectX", 1);
        CropIntent.putExtra("aspectY", 1);
        CropIntent.putExtra("scale", true);
        CropIntent.putExtra("return-data", true);

        startActivityForResult(CropIntent, PERM_CODE);


    } catch (ActivityNotFoundException ex){

    }
}

谁能说出如何获得输出uri?感谢。

1 个答案:

答案 0 :(得分:0)

如果裁剪后的图像显示在ImageView中,请按this link将其保存到设备存储空间,然后即可上传文件。