Как обрезать изображение в android

Я хочу использовать этот метод для кадрирования изображения в android

И я нашел код на странице

Но есть проблема

Я не знала как получить фото

private void performCrop(Uri picUri) {
        try {
            Intent cropIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            cropIntent.setDataAndType(picUri, "image/*");
            cropIntent.putExtra("crop", true);
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", 128);
            cropIntent.putExtra("outputY", 128);
            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, PIC_CROP);
        }
        catch (ActivityNotFoundException anfe) {
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PIC_CROP) {
            if (data != null) {
                // get the returned data
                Bundle extras = data.getExtras();
                // get the cropped bitmap
                Bitmap selectedBitmap = extras.getParcelable("data");

                img.setImageBitmap(selectedBitmap);
            }
        }
    }

Помог ли мой ответ?

Ishaan Javali 07.12.2018 01:26
0
1
51
1

Ответы 1

Вот как это можно сделать: Приведенный ниже метод берет растровое изображение и возвращает uri.

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
} 

Вот как будет выглядеть ваш код:

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

    if (requestCode == PIC_CROP) {
        if (data != null) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");
            //get the Uri when it is returned from the function.
            picUri = getImageUri(getApplicationContext(), selectedBitmap);
            img.setImageBitmap(selectedBitmap);
        }
    }
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
} 

Это должно быть так, как вы получаете Uri из растрового изображения. Надеюсь, что это работает!

что такое inImage?

Ishaan Javali 07.11.2018 15:14

Ошибка, которую я использую из намерения (Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_UR‌ I);

user9862540 07.11.2018 15:53

Попробуйте получить Uri с помощью метода, который я дал вам выше. Это будет работать. Получив растровое изображение из намерения, вы можете передать его методу для получения Uri.

Ishaan Javali 07.11.2018 16:04

Другие вопросы по теме