我正在拍摄如下照片:
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;
}
}
}
}
答案 0 :(得分:1)
注意:对于api-24或更高版本,see here。
定位api-23或更低版本
执行此操作的最佳方法是使用用于打开相机的Intent发送Uri。 您需要创建一个临时文件,该文件将被拍摄的照片覆盖。
首先,声明一个成员变量来存储Uri:
url http://192.168.3.42:8004/glue/BladeCommandService
electric.util.WrappedException: java.rmi.ConnectException: url = http://169.254.245.248:8004/glue/BladeCommandService
at electric.proxy.handler.Proxy.getCompatibleException(Unknown Source)
at electric.proxy.handler.Proxy.invoke(Unknown Source)
at $Proxy12.executeCommandOnBlade(Unknown Source)
at com.clearcube.glue.GBladeCommandService.executeCommandOnBlade(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
设置临时文件并将Uri传递给相机应用程序:
public class MyPhotoActivity extends AppCompatActivity {
Uri uri;
//.............
然后,如果在onActivityResult()中获得成功结果,则创建的临时文件具有捕获的图像,您可以将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);
}
用于所需的任何内容:
uri