nullpointerexception when not taking photo - java

I have an app where at an activity I am taking a photo (among other things) .
Now, when I press the button to take the photo it opens the camera.If i will press the back button or the cancel button (not taking photo) ,it crashes and gives
nullpointer
and
Failure delivering result ResultInfo
in this line:
Bitmap photo = (Bitmap) data.getExtras().get("data");
I use:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
blobvalue = stream.toByteArray();
Bundle extras = new Bundle();
Intent k=new Intent(this,MainActivity.class);
extras.putParcelable("Bitmap", photo);
k.putExtras(extras);
}
if (requestCode == RESULT_CANCELED) {
}
}
and in my adapter:
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
final Bitmap image;
if(theItems.getImagemyItems() != null)
{
byte []temp = theItems.getImagemyItems();
image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
myImage.setImageBitmap(image);
}
else
{
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
myImage.setImageBitmap(image);
}
As far as I remember , the above used to workd for this purpose.
I don't know what else to do.

You have just tested requestCode but haven't resultCode so I would suggest you to check resultCode whether user has captured image or cancel capturing.
Try:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}

You just have to place a check in your onActivityResult , the case RESULT_OK is when the user takes the picture successfully and the case RESULT_CANCELLED is when you press the hardware back button and want to return to your activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
// your code comes here
}
if(resultCode == RESULT_CANCELED){
}
}
}

Related

Automatically picks image from gallery according to its name java

I want to create an application that picks the image from gallery automatically according to its name. The user needs to sign in a signaturepad that he can save on the device and when he clicks on the button to save the image, the app automatically picks this image from gallery and display it on an imageview. I created one where I need to pick the image manually (with a little bit of help), this is my code so far:
private void SelectImage()
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); //Can I define here the name of the image that I want to be selected?
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data){
imageUri = data.getData();
imageView1.setImageURI(imageUri);
}
}

Custom Gallery Image Picker

I would like to allow the user to pock only one image that can be referenced via a uri, I've successfully done this through the following code:
private static final int PICK_IMAGE_REQUEST = 1;
private Uri imageUri;
// Choose file extended from BottomTabView, opens all images on device
public static void openFileChooser(Context context) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(intent, PICK_IMAGE_REQUEST);
// Slide Animation
((Activity) context).overridePendingTransition(R.anim.slide_in_up, R.anim.nothing);
}
// TODO: Is it better to use bitmap or URI
// Check if user has selected file and describe next step
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
// Retrieve image as a URI
imageUri = data.getData();
// Pass image URI to an intent and start activity
Intent intent = new Intent(this, UploadImageActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
// Slide Animation
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
this.finish();
}
}
The above code opens the following:
However, I would like to have something like the following:
Question: How can I achieve something more like the "Custom Gallery"?
if you want to custom layout pick image, you need:
create a screen and using recyclerView to make the layout as you want
get all picture gallery (https://stackoverflow.com/a/25957752/10153377)

selecting photos from google photos crushes my app

i'm writing an android app on java and need to let my users select and crop images from the gallery.
There is no problem when choosing an image from any native gallery, but when a user chooses to eater crop or choose an image from google photos app the app crushes.
I cannot figure out what is the source of the problem so any answer will be helpful
this is the code i'm using
class fields:
private Uri imageUri;
opening the camera:
private void camOpen() {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(), "file" + String.valueOf(System.currentTimeMillis()) + ".png");
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
imageUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
i.putExtra("return-data", true);
startActivityForResult(i, CAMERA_CODE);
}
opening the gallery:
private void galleryOpen() {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, "select file"), SELECT_PHOTO_CODE);
}
cropping the image:
private void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(imageUri, "image/*");
cropIntent.putExtra("crop", true);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_CODE);
} catch (Exception e) {}
}
the result handler:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CODE && resultCode == Activity.RESULT_OK) {
cropImage();
} else if (requestCode == SELECT_PHOTO_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
imageUri = data.getData();
cropImage();
}
} else if (requestCode == CROP_CODE && resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap b = bundle.getParcelable("data");
hasImageChanged=true;
ivProfilePic.setImageBitmap(b);
capturedImage = b;
}
}
thank you for any useful help...
Android does not support crop intent because croping is not part of android api.
So i recommend you for Using library
the issue was with the crop intent. I ended up using the uCrop library and it fixed the problem.

Android: Multiple image view

I want two ImageView's on one activity view, One image for the profile picture and the other for the profile cover page.
My code
setupImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bringImagePicker();
}
});
private void bringImagePicker() {
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(SetupActivity.this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
Now how can I add for profile cover image?
You can use ID for both of them, and change the image with corresponding ID from the intent that holds data, add another condition after receiving the results.
I don't know if this library can handle this or not.
If not this simplest solution to hold state variable that tells you which image to update
feel free to ask for clarification

onActivityResult byte[] data returning null no matter how large the image

I am trying to pass a byte array between activities using startActivity and onActivityResult. The byte array is null when returning to onActivityResult and I cannot figure out why. This happens no matter how large the byte array is so I don't think it has to do with size. Also, I am passing a byte array of approximately the same size using intents successfully in another area. The code:
In post activity:
public void callCropperIntent() {
/* call the cropper to crop a photo from the gallery */
intent = new Intent(getApplicationContext(), Cropper.class);
startActivityForResult(intent, CROP_GALLERY_PICTURE);
}
In cropper activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
File croppedImageFile = new File(getFilesDir(), "test.jpg");
try {
if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) {
/** When the user is done picking a picture, we'll start the CropImage Activity,
* setting the output image file and size to 640 X 640 pixels square.
*/
Uri croppedImage = Uri.fromFile(croppedImageFile);
CropImageIntentBuilder cropImage = new CropImageIntentBuilder(640, 640, croppedImage);
cropImage.setSourceImage(data.getData());
cropImage.setOutputQuality(100);
startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
}
else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) {
/* when we are done cropping, send it back to PostActivity */
//imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath()));
Bitmap bmp = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//this set bitmap works so I know the bitmap is valid
//test
//imageView.setImageBitmap(bmp);
byte[] imageData = stream.toByteArray();
//bmp.recycle();
Intent intent = getIntent();
intent.putExtra("image", imageData);
setResult(RESULT_OK, intent);
finish();
}
}
Back in post activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
byte[] croppedData;
/* check the request code */
if (requestCode == CROP_GALLERY_PICTURE) {
/* ensure the request was successful */
if (resultCode == RESULT_OK) {
/* The user picked and cropped a photo */
/* retrieve photo from cropping activity */
Intent intent = getIntent();
croppedData = data.getByteArrayExtra("image");
/* bytes ready to be sent to pg. 2 of posting process */
callPostActivityPg2Intent(croppedData);
}
}
}
I think in the cropper class,before finish() you should create a new instance of Intent class instead of obtaining one by calling getintent().
Ex: Intent intent = new Intent();
intent.putExtra ()....

Categories