android 相册 uri空,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();
}
在此做一記錄!
本文同步分享在 博客“Trent1985”(CSDN)。
如有侵權,請聯系 support@oschina.cn 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。
總結
以上是生活随笔為你收集整理的android 相册 uri空,Android---相册getContentResolver().query结果为空指针的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于MATLAB的特殊函数与画图(附图像
- 下一篇: springboot 配置webserv