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;
}
}
}
}