下面是我的代码但是没有给我onActivityresult
中的图像路径Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.w("jay", "Camera Image Path :" + selectedImagePath);
Toast.makeText(MiscansOther_pannel.this, selectedImagePath,
Toast.LENGTH_LONG).show();
答案 0 :(得分:41)
这对我有用......
<强>代码:强>
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 for capture image作为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,