In my application I want cropped the image and then convert this to bitmap
.
For crop image I use this library : https://github.com/jdamcd/android-crop
I want when call onActivityResult
convert this image to bitmap
and for this I write below codes :
@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();
}
}
And this is my method for convert bitmap to 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);
}
But when running application show me below error message in 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)
How can I fix it? please help me.
I am amateur and really need your help. Thanks
Use this code to get the result.
Uri imageUri = Crop.getOutput(result);
And convert this imageUri to bitmap.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Pass this bitmap instance to your base64 coder.