Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult returns null data for an Image Capture

@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.

like image 637
Archie.bpgc Avatar asked Jul 24 '13 07:07

Archie.bpgc


2 Answers

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.

like image 98
alex Avatar answered Oct 19 '22 11:10

alex


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();
                }
            }
like image 1
Gaurab Avatar answered Oct 19 '22 09:10

Gaurab