1

I am doing an android app, in which I have a functionality that, I need to open camera and take a picture. After taking picture onActivityResult is not called. my screen remains only in camera state.

My code:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaFile = new File(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
Uri imageUri = Uri.fromFile(mediaFile);
         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
         startActivityForResult(intent, 1);

onActivityResult code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null && !data.toString().equalsIgnoreCase("Intent {  }"))
        switch (requestCode) {
        case 1:
            Log.i("StartUpActivity", "Photo Captured");
             Uri uri = data.getData();
             String imgPath = getRealPathFromURI(uri);
            bitmap = (Bitmap) data.getExtras().get("data");
            MediaStore.Images.Media.insertImage(getContentResolver(),
                    bitmap, null, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            img1.setImageBitmap(bitmap);
}
}
user2085965
  • 393
  • 2
  • 13
  • 33
  • make the `super.onActivityResult()` the last instruction on your `onActivityResult()` overrided method. refer this guide of android to check it : http://developer.android.com/guide/topics/media/camera.html Try to add some log to verify if the onActivityResult is really called or not – Houcine Dec 30 '13 at 18:31
  • I have answered this question before take a look : http://stackoverflow.com/a/30266776/1531683 – sadegh saati May 15 '15 at 18:52

7 Answers7

0

onActivityResult is probably called, but the statements inside your if statement and switch/case statements are not being executed, because they are never reached.

The code below includes two methods takePhoto and onActivityResult, is very short and works perfectly. Take a look at it, it probably will help you!

public void takePhoto(View v) {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
  }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
      photo = (Bitmap) data.getExtras().get("data");
      imageView.setImageBitmap(photo);
    }
  }
Karim
  • 5,298
  • 3
  • 29
  • 35
0

You can run your application in debug mode, after putting a breakpoint in onActivityResult method, if it's stopping there after getting from the Camera then it's being called successfully.

and you could just change the if statement you're using making your code like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null && resultCode = RESULT_OK)
        switch (requestCode) {
        case 1:
            Log.i("StartUpActivity", "Photo Captured");
             Uri uri = data.getData();
             String imgPath = getRealPathFromURI(uri);
            bitmap = (Bitmap) data.getExtras().get("data");
            MediaStore.Images.Media.insertImage(getContentResolver(),
                    bitmap, null, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            img1.setImageBitmap(bitmap);
      }
}

and note that it is more recommended to using final ints as the request code instead of rewriting them everytime they're needed like :

private final int START_CAMERA_REQUEST_CODE = 1;
Triple.B
  • 144
  • 1
  • 1
  • 7
0

I think this may be help you

Follow Below Code step by step and compare with your code to avoid null exception

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);

1 OnActivity Result

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

    if (resultCode == Activity.RESULT_OK) {
     if (requestCode == 1)
            onCaptureImageResult(data);
    }
}

2 onCaptureImageResult

 public void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    Uri tempUri = getImageUri(getApplicationContext(), thumbnail); // call getImageUri

    File finalFile = new File(getRealPathFromURI(tempUri));
    file_path = finalFile.getPath().toString(); // your file path
    imgview.setImageBitmap(thumbnail);
   // this code will get your capture image bitmap}

3 getImageUri

 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);  // your capture image URL code
    return Uri.parse(path);

}

Using this code your screen will not remain more on camera state it will finish and image will be load as well you get image URL.

-1
Bitmap mBitmap = BitmapFactory.decodeFile(imgPath);

pass Image path which you create from URI.

Houcine
  • 24,001
  • 13
  • 56
  • 83
-1

Probably, your activity is reloaded after camera is finished, but your code is not prepared to such situation.

See Android startCamera gives me null Intent and ... does it destroy my global variable?.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
-1

I think onActivity is called but it doesn't go into the if condition

Check only for null in if condition:

 if (data != null )
    switch (requestCode) {
    case 1:
        Log.i("StartUpActivity", "Photo Captured");
         Uri uri = data.getData();
         String imgPath = getRealPathFromURI(uri);
        bitmap = (Bitmap) data.getExtras().get("data");
        MediaStore.Images.Media.insertImage(getContentResolver(),
                bitmap, null, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        img1.setImageBitmap(bitmap);
}
AndyB
  • 391
  • 2
  • 4
  • 17
-1

From where you are calling startActivityForResult(intent, 1), and where you Overriding onActivityResult() ?

I mean if you call startActivityForResult() from Frgment then you should override onActivityResult() in fragment it self, same applies to Activity also.

Ram Prakash Bhat
  • 1,308
  • 12
  • 21