裁剪后如何将图像转换为位图?

How to convert image to Bitmap after it's been cropped?

在我的应用程序中,我想要 裁剪 图像,然后将其转换为 bitmap
对于裁剪图像,我使用这个 library : https://github.com/jdamcd/android-crop

我想在调用 onActivityResult 时将此图像转换为 bitmap 为此,我 编写以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
        beginCrop(result.getData());
    } else if (requestCode == Crop.REQUEST_CROP) {
        handleCrop(resultCode, result);
    }
}

private void beginCrop(Uri source) {
    Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
    Crop.of(source, destination).asSquare().start(this);
}

private void handleCrop(int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {

        if (result != null) {
            Bundle bundle = result.getExtras();
            Bitmap bitmap = bundle.getParcelable("data");
            //imageView.setImageBitmap(bitmap);
            base64String = ImageConvertBase64.convert(bitmap);

            //Edit base64 for site
            base64StringForSite = "data:image/png;base64," + base64String;

            //Upload image call api
            UploadAvatarImageSendData sendData = new UploadAvatarImageSendData();
            sendData.setBase64ImageData(base64StringForSite);

            profileEdit_avatarProgressBar.setVisibility(View.VISIBLE);

            Call<SendCommentResponse> call = api.getUserUploadAvatar(token, "2", sendData);

            call.enqueue(new Callback<SendCommentResponse>() {
                @Override
                public void onResponse(Call<SendCommentResponse> call, Response<SendCommentResponse> response) {
                    if (response.body().getData() != null) {
                        profileEdit_avatarProgressBar.setVisibility(View.GONE);
                        getUpdateAvatarImage();
                        Toasty.success(context, response.body().getStatusMessage(), Toast.LENGTH_SHORT, true).show();
                    } else {
                        Toasty.info(context, context.getResources().getString(R.string.retryAgainAgain), Toast.LENGTH_SHORT, true).show();
                    }
                }

                @Override
                public void onFailure(Call<SendCommentResponse> call, Throwable t) {
                    profileEdit_avatarProgressBar.setVisibility(View.GONE);
                    Log.e("uploadImage", "Err : " + t.getMessage());
                }
            });
        }

    } else if (resultCode == Crop.RESULT_ERROR) {
        Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
    }
}

这是我将位图转换为 base64 的方法:

public static String convert(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

但是当 运行 应用程序在 LogCat 中显示下面的 错误消息 时:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at com.example.app.Utils.Componenets.ImageConvertBase64.convert(ImageConvertBase64.java:25) at com.example.app.Activities.ProfileEditActivity.handleCrop(ProfileEditActivity.java:326) at com.example.appt.Activities.ProfileEditActivity.onActivityResult(ProfileEditActivity.java:192) at android.app.Activity.dispatchActivityResult(Activity.java:6222) at android.app.ActivityThread.deliverResults(ActivityThread.java:3627)

我该如何解决?请帮我。

我是业余爱好者,非常需要您的帮助。谢谢

使用此代码获取结果。

Uri imageUri = Crop.getOutput(result);

并将此 imageUri 转换为位图。

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);

将此位图实例传递给您的 base64 编码器。