Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_IMAGE_CAPTURE - get URI after taking a photo at the default location

I'm taking a picture like following:

activity.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), BaseDef.REQUEST_CODE_CAMERA);

Afterwards, I just want to get the new images path, uri or similar (NOT the bitmap or image data), so that I know where the new image is located. Therefore I try to retrieve the uri like following:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == BaseDef.REQUEST_CODE_CAMERA)
    {
        if (resultCode == RESULT_OK)
        {
            Uri uri = data.getData();
        }
    }
}

But the uri sometimes is null. How can I get the image path/uri on such devices?

EDIT

I really want to keep the default file name created by whichever camera app is used!

Current workaround (I don't know if this always works):

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == BaseDef.REQUEST_CODE_CAMERA)
    {
        if (resultCode == RESULT_OK)
        {
            String path = null;
            Uri uri = data.getData();
            if (uri != null)
                path = MediaUtil.getRealPathFromURI(uri);
            else
            {
                L.d(this, "URI == NULL => trying to get last image path directly");
                // here I just sort mediastore by id desc and get the first image's path
                // can I be sure this ALWAYS works? 
                // When the camera returns, the image SHOULD be already in the media store
                // If not, I would get a wrong image...
                path = MediaStoreUtil.getLastImagePath(); 
            }

            if (path == null)
            {
                L.d(this, "Path of camera image could not be retrieved");
                return;
            }
        }
    }
}
like image 853
prom85 Avatar asked Nov 12 '15 07:11

prom85


1 Answers

NOTE: For targeting api-24 or higher, see here.

For targeting api-23 or lower:

The best way to do this is to send the Uri with the Intent used to open the camera. You will need to create a temp file that will be overwritten with the photo taken.

First, declare a member variable to store the Uri:

public class MyPhotoActivity extends AppCompatActivity {

    Uri uri;
    //.............

Setting up the temp file and passing the Uri to the camera app:

public void takePhoto() {
  counter++; //this is an int
  String imageFileName = "JPEG_" + counter; //make a better file name
  File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  File image = File.createTempFile(imageFileName, 
                                   ".jpg", 
                                   storageDir
                                  );

  uri = Uri.fromFile(image);
  Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  startActivityForResult(takePhotoIntent, BaseDef.REQUEST_CODE_CAMERA);
}

Then, if you get a success result in onActivityResult(), the temp file created has the captured image, and you can use uri for whatever you need:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == BaseDef.REQUEST_CODE_CAMERA)
    {
        if (resultCode == RESULT_OK)
        {
            //use uri member variable...
            //it has the image that was captured
        }
    }
}
like image 81
Daniel Nugent Avatar answered Sep 28 '22 09:09

Daniel Nugent