下面是我的代码但是没有给我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); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } catch (Exception e) { return contentUri.getPath(); } }
编辑:
正如我在一些设备中注意到捕获的图像后,onActivityResult()中的数据为空,
因此,替代方法是将特定图像文件名作为参数传递给您的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);
在这种情况下,它将在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,