onActivityResult returns null data for an Image Capture
Asked Answered
J

5

10
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    filePath = getOutputMediaFile(FileColumns.MEDIA_TYPE_IMAGE);
    File file = new File(filePath);
    Uri output = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, output);
    startActivityForResult(i, RETURN_FILE_PATH);
}

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

    //data is always null here.
    //requestCode = RETURN_FILE_PATH;
    //resultCode = Activity.RESULT_OK;
}

I checked the values for file and output Uri, both are fine and the captured image actually exists at that location.

But the data returned in onActivityResult is always null even after capturing the image.

EDIT:

I checked this question:

onActivityResult returns with data = null

which says:

Whenever you save an image by passing EXTRAOUTPUT with camera intent the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.

and maybe that solution will work for me. But the above code of mine was a working code until now for the same scenario.

Jere answered 24/7, 2013 at 7:13 Comment(0)
C
20

According to this post data is null when you pre insert a uri. That means you already defined your output uri here:

  i.putExtra(MediaStore.EXTRA_OUTPUT, output);

So when you get a Activity.RESULT_OK; just load the taken photo by its known url.

Clarissaclarisse answered 24/7, 2013 at 7:24 Comment(2)
Yeah just saw that post. But the same code worked for the past 6 months why all of a sudden now it behaves this way :(Jere
Unfortunately i am not expert on this topic put what i guess that its a device specific functionality related to the hardware. This post mentions that there is a different behaviour on Samsung devices: https://mcmap.net/q/555138/-my-android-camera-uri-is-returning-a-null-value-but-the-samsung-fix-is-in-place-help/1965084Clarissaclarisse
G
1

Try this code this is working for me.

else if(requestCode == Constant.PICK_FROM_CAMERA)
            {

                if (resultCode == Activity.RESULT_OK) 
                {
                    if(data!=null)
                    {
                        mImageCaptureUri = data.getData();
                        //path= mImageCaptureUri.getPath();
                        try
                        {
                            path = getPath(mImageCaptureUri,Wonderlistpage.this); //from Gallery
                        }
                        catch(Exception e)
                        {
                            path = mImageCaptureUri.getPath();
                            Log.i("check image attach or not", e.toString());
                        }

                        String arr[] = path.split("/");
                        int i;
                        String k = null;
                        for(i=0;i<arr.length;i++)
                        {
                            k=arr[i];       
                        }
                        photoname="_"+String.valueOf(System.currentTimeMillis()) +k;
                         if(setprofileimage_sendimagewithmessage==1)
                         {
                             performCrop(mImageCaptureUri);
                         }
                         else
                         {
                              loading_details="CAMERA";
                              new performBackgroundTask33().execute();
                         }
                    }
                    else
                    {
file1  = new File(Environment.getExternalStorageDirectory(),
                                    String.valueOf(System.currentTimeMillis()) + "_FromCamera.jpg");

                        Uri mImageCaptureUri = Uri.fromFile(file1);
                        try
                        {
                            path = getPath(mImageCaptureUri,Wonderlistpage.this); //from Gallery
                        }
                        catch(Exception e)
                        {
                            path = mImageCaptureUri.getPath();
                            Log.i("check image attach or not", e.toString());
                        }
                        String arr[] = path.split("/");
                        int i;
                        String k = null;
                        for(i=0;i<arr.length;i++)
                        {
                            k=arr[i];       
                        }
                        photoname="_"+String.valueOf(System.currentTimeMillis()) +k;
                         if(setprofileimage_sendimagewithmessage==1)
                         {
                             performCrop(mImageCaptureUri);
                         }
                         else
                         {
                              loading_details="CAMERA";
                              new performBackgroundTask33().execute();
                         }

                    }

                    //new UploadTask().execute();
                }
            }
Glory answered 3/2, 2014 at 5:47 Comment(0)
C
1

Try following code

        {
            final String[] imageColumns = { MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA };

            final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
            Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
            imageCursor.moveToFirst();
            do {
                String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                if (fullPath.contains("DCIM")) {

                    //get bitmap from fullpath here.
                    return;
                }
            }
            while (imageCursor.moveToNext());
Cesarcesare answered 3/9, 2015 at 7:57 Comment(0)
P
0

Just Put this code into your onActivityResult. The same problem i have faced on some devices and this solved my problem. Hope this will also help you.

try {

    Uri selectedImage = output;

    if (selectedImage == null)
        return;

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();

} catch (Exception e) {
    return;
}     

You will get the Picture path in picturePath variable and Uri in selectedImage Variable.

Portland answered 24/7, 2013 at 9:4 Comment(0)
I
0

If your activity has launchmode as singleInstance in your manifest then you would face this issue. Try changing it. As it cancels the result everytime.

Ianiana answered 4/7, 2016 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.