Android 如何获取刚刚从相机捕获的图像路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11591825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Image Path just captured from camera
提问by Jaykumar Donga
below is my code but is not give me image path in onActivity result
下面是我的代码,但没有在 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();
回答by user370305
This works for me...
这对我有用...
Code:
代码:
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
Method: getRealPathFromURI()
方法: 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();
}
}
EDIT:
编辑:
As I noticed in some device after captured image the data in onActivityResult()
is null,
正如我在捕获图像后在某些设备中注意到的那样,数据onActivityResult()
为null,
So the alternate way, Pass the specific image filename as a argument to your Intent for capture image as putExtra
parameter.
因此,另一种方法是,将特定图像文件名作为参数传递给您的 Intent 以作为putExtra
参数捕获图像。
Then also insert this image Uri in Media Store, Now use this Uri for your further use,
然后也在 Media Store 中插入此图像 Uri,现在使用此 Uri 供您进一步使用,
You can check whether image is captured or not by File.exist()
,
您可以检查图像是否被捕获File.exist()
,
Code looks like,
代码看起来像,
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);
Now, you can use the same method for get file path from Uri,
现在,您可以使用相同的方法从 Uri 获取文件路径,
in this case it will be in onActivityResult()
,
在这种情况下,这将是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,