【发布时间】:2026-02-13 22:30:02
【问题描述】:
我正在拍一张像下面这样的照片:
activity.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), BaseDef.REQUEST_CODE_CAMERA);
之后,我只想获取新的图像路径,uri 或类似的(不是位图或图像数据),以便我知道新图像的位置。因此,我尝试检索 uri,如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == BaseDef.REQUEST_CODE_CAMERA)
{
if (resultCode == RESULT_OK)
{
Uri uri = data.getData();
}
}
}
但有时 uri 是 null。如何在此类设备上获取图像路径/uri?
编辑
我真的很想保留使用哪个相机应用程序创建的默认文件名!
当前的解决方法(我不知道这是否总是有效):
@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;
}
}
}
}
【问题讨论】: