简体   繁体   中英

onActivityResult is not calling after taking camera picture

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);
}
}

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);
    }
  }

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;

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.

Bitmap mBitmap = BitmapFactory.decodeFile(imgPath);

传递您从URI创建的图像路径。

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? .

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM