uri = data.getData () is null in Jelly bean

2

I am developing an application in Android Studio and testing it with my smartphone 4.2.2, I need to take a photo first and get its Uri to then cut it out. On the internet I found that for that it is used:

url = data.getData();

But in the same way I investigated that in Jelly bean this does not work like that, and indeed because what I get as a result is NULL even though the photo does capture it. I've been trying everything for hours but until now I can not find something to solve the problem. Is there another way to get the Uri? Thanks

Annex the rest of the code just in case.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_eslyss);
    boton = (Button) findViewById(R.id.button);

    boton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                dispatchTakePictureIntent();
        }
    });
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

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

        uri = data.getData();

        Intent intent = new Intent();
        intent.setClass(Activity.this, Salto1.class);
        intent.putExtra("Bitmap", bitmap2);
        startActivity(intent);


    }

}
    
asked by Fernando O 15.05.2017 в 21:43
source

2 answers

0

You have to add EXTRA_OUTPUT to your Intent% % co where you define the Uri of the temporary file:

 Uri captureImage = Uri.fromFile(getTempFile());
 intent.putExtra(MediaStore.EXTRA_OUTPUT, captureImage);

would be like this:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

     Uri captureImage = Uri.fromFile(getTempFile());
     intent.putExtra(MediaStore.EXTRA_OUTPUT, captureImage);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}
  

EXTRA_OUTPUT The name of the Intent-extra is used to indicate   a content resolution Uri that will be used to store the   Image or video requested.

    
answered by 15.05.2017 в 23:40
0

Surely you have the same problem as in this post

Indeed, you have to pass the Uri in the Intent that you use to open the camera, but if you do it using Uri.fromFile() it will give you android.os.FileUriExposedException if you have targetSdkVersion = 24 , so I recommend using < to href="https://developer.android.com/reference/android/support/v4/content/FileProvider.html"> FileProvider to avoid it.

    
answered by 13.06.2017 в 16:21