Android---相册getContentResolver().query结果为空指针

针对小米4手机上测试如下代码:

Uri uri = data.getData();
String[] proj = {MediaStore.Images.Media.DATA};
//Cursor cursor = managedQuery(uri, proj, null, null, null);
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
cursor.moveToFirst();
	int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	String picturePath = cursor.getString(columnIndex);
	cursor.close();
	mImgPath = picturePath;


cursor的结果为null,其他手机没有问题,查找原因,得到如下结论:

对于android 4.4版本及以后,uri的形式发生了变化,此时如果要获取图像的路径,可以按照如下实现:

Uri selectedImage = data.getData();
 String[] filePathColumn = { MediaStore.Images.Media.DATA };
		    			 
 Cursor cursor = getContentResolver().query(selectedImage,
	filePathColumn, null, null, null);
if(cursor!=null)
{
	cursor.moveToFirst();
	int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	String picturePath = cursor.getString(columnIndex);
	cursor.close();
	mImgPath = picturePath;
}
else
{
	mImgPath = selectedImage.getPath();
}

在此做一记录!


你可能感兴趣的:(Android---相册getContentResolver().query结果为空指针)