Android data.getData () returns null from CameraActivity for some phones

I have a fatal error occurring in my onActivityResult returning from camera activity. What I scratch my head is that the error only occurs on a few phones (depending on the number of users affected), although for most there is nothing wrong. I can duplicate the error on my Nexus 6 (Lollipop 5.1.1 works), while my note 5 (also 5.1.1) has no problems at all.

The problem is that I am trying to assign imageUri from data.getData (). Debugging on note 5, data.mData is "content: // media / external / images / media / 2215", and on Nexus 6, data.mData is null.

I know this is a general question asked on SO, but so far I have not found anything that helped me. Can someone point me to this solution and give an answer?

Method Trigger the camera action for the result

@OnClick(R.id.change_image_camera) public void takePicture(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);}

onActvityResult

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

    if (resultCode == Activity.RESULT_OK) {

        Uri imageUri = data.getData(); //The trouble is here

        String realPath = Image.getPath(this, imageUri); //getPath crashes because imageUri is null

        Image.compressImage(realPath);

        File file = new File(realPath);

        Bundle extra = new Bundle();
        extra.putString("URL", realPath);
        returnIntent.putExtras(extra);

        setResult(RESULT_OK, returnIntent);
        finish();
    }
}

I really appreciate any help with this!

+4
source share
1 answer
Uri imageUri = data.getData(); //The trouble is here

It is not required that the camera application return Uripointing to a photograph, as this is not part of the protocol . Or: ACTION_IMAGE_CAPTURE Intent

  • Put EXTRA_OUTPUTin ACTION_IMAGE_CAPTURE Intent, in which case you know where the image should be stored, and you do not need to rely on getData()or

  • Use dataextra in the answer Intent, which will be a Bitmapthumbnail image

Your next error is here:

String realPath = Image.getPath(this, imageUri);

, , EXTRA_OUTPUT.

+4

Source: https://habr.com/ru/post/1605980/


All Articles