Android Jelly bean camera return null URI

2019-08-07 07:27发布

问题:

I'm trying to use the camera to take pictures in my app, and then crop the taken images.

Everything is working for the recent versions for Android, but not for Android Kitkat 4.4.2.

the Camera returns a null URI.

get the URI onActivityReslult :

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_CODE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");

            picUri = data.getData();
            Intent i = new Intent(PublierActivity.this, CropActivity.class);
            i.putExtra("Uri", picUri);
            startActivityForResult(i, CROP_CODE);

        }

Here is how I call the camera intent :

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, CAMERA_CODE);
                }

is there any way to make an exception for oldest versions for android to resolve this problem ?

回答1:

From your code, it would appear that you are trying to use ACTION_IMAGE_CAPTURE. In that case, there should never be a Uridata.getData() should always be returning null. If a camera app happens to return a Uri, that may be the image, but since ACTION_IMAGE_CAPTURE is not documented to return a Uri, you have no way of knowing what that Uri is for.

If you are using EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent, you know where the image should be stored, because you told the camera app where to store it. Note that some camera apps are buggy and fail to honor EXTRA_OUTPUT, putting the image wherever they want.

If you are not using EXTRA_OUTPUT, then you will get a thumbnail back in the "data" extra.

Also, please bear in mind that this has nothing to do with Android OS version, and everything to do with the camera app that the user chooses to use. There are thousands of Android device models. These ship with dozens, if not hundreds, of different camera apps pre-installed. The user might also elect to install a third-party camera app. Any of those could be handling your request.