onActivityResult returning Intent data.getData(); always Null Only in Marshmallow and Lollipop

你离开我真会死。 提交于 2019-12-01 02:17:30

When we will capture the image from Camera in android then Uri or data.getdata() comes null. we have two solutions to resolve this issue.

  1. We can got the Uri path from the Bitmap Image
  2. We can got the Uri path from cursor.

I will implement all methods here, Please carefully watch and read these:-

First i will tell how to get Uri from Bitmap Image: Complete code is :

First we will capture image through Intent that will same for both methods so this code i will write one time only here :

 // Capture Image
        captureImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, reqcode);
                }

            }
        });

Now we will Implement OnActivityResult :-(This will be same for both above 2 methods):-

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


        if(requestCode==reqcode && resultCode==RESULT_OK)
        {

Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView.setImageBitmap(photo);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            \\ Show Uri path based on Image
            Toast.makeText(LiveImage.this,"Here "+ tempUri, Toast.LENGTH_LONG).show();

           \\ Show Uri path based on Cursor Content Resolver
            Toast.makeText(this, "Real path for URI : "+getRealPathFromURI(tempUri), Toast.LENGTH_SHORT).show();
}
        else
        {
            Toast.makeText(this, "Failed To Capture Image", Toast.LENGTH_SHORT).show();
        }
    }

\now we will create all above method to create Uri from Image and Cursor methods via classes:

Now URI path from Bitmap Image

  private Uri getImageUri(Context applicationContext, Bitmap photo) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(LiveImage.this.getContentResolver(), photo, "Title", null);
        return Uri.parse(path);
    }

\ Uri from Real path of saved image

  public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }

Dear Friend copy and paste this code and then try to understood. I'm 100 % sure this will better for you if you are making corporate Apps. If you like my code then vote for me....

It is working perfectly on every Phone except Marshmallow and Lollipop

No, it is not. It will fail on lots of Android versions, when the user chooses a well-written camera app to handle your ACTION_IMAGE_CAPTURE request.

Your problem lies here:

            if (data.getData()!=null){
            picturePath=getPath(column_Name,data.getData());}
            else {
                //My app Crashes here because in Marshmallow data.getData() is always null.
            }

There are at least two flaws here.

The big one is assuming that you get a Uri back from ACTION_IMAGE_CAPTURE. That is not documented, and camera apps do not need to return a Uri. In particular, in your structure, you will only get a thumbnail back, via getExtra("data"). If you want a full-size image, use EXTRA_OUTPUT on your Intent, in which case you know where the image is being stored — it is wherever you are indicating in EXTRA_OUTPUT.

The other one is that you assume that the Uri that you get back is from the MediaStore or otherwise has a MediaStore.Video.Media.DATA column. Not only do camera apps not have to return a Uri, but there is no requirement that the Uri is from the MediaStore or have such a column.

As,suggested by @CommonsWare -

Camera App do not need to return uri.

Also,

You need to tell Camera app where to write image.

So, i replaced my code with-

 final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            pictureUri=getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

and in onActivityResult -

 case REQUEST_IMAGE_CAPTURE:

            if (resultCode==RESULT_OK){
                String picturePath="";
                String column_Name= MediaStore.Images.Media.DATA;
                if (data!=null){
                if (data.getData()!=null){
                picturePath=getPathfromUri(column_Name,data.getData());}

                else {
                    picturePath= pictureUri.getPath();

                }
                }
                else {
                    picturePath= pictureUri.getPath();

                }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!