[英]How to get Image Path just captured from camera
以下是我的代码,但未在onActivity结果中提供图像路径
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.w("jay", "Camera Image Path :" + selectedImagePath);
Toast.makeText(MiscansOther_pannel.this, selectedImagePath,
Toast.LENGTH_LONG).show();
这对我有用...
码:
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
方法: getRealPathFromURI()
//----------------------------------------
/**
* This method is used to get real path of file from from uri
*
* @param contentUri
* @return String
*/
//----------------------------------------
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
编辑:
正如我在捕获图像后在某些设备中注意到的那样, onActivityResult()
的数据为null ,
因此,另一种方法是将特定的图像文件名作为参数传递给您的用于捕获图像的Intent作为putExtra
参数。
然后还将这张图片Uri插入Media Store中,现在使用此Uri进行进一步使用,
您可以检查File.exist()
是否捕获了图像,
代码看起来像
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);
现在,您可以使用相同的方法从Uri获取文件路径,
在这种情况下,它将位于onActivityResult()
,
selectedImagePath = getRealPathFromURI(mCapturedImageURI); // don't use data.getData() as it return null in some device instead use mCapturedImageUR uri variable statically in your code,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.