#获取系统图片遇到的坑--Cursor空指针异常

来源:互联网 发布:学而知之什么意思 编辑:程序博客网 时间:2024/06/05 17:11

获取系统图片遇到的坑–Cursor空指针异常

Cursor 空指针异常:
小米N2测试的时候报Cursor空指针异常的错误

   Uri photoUri = data.getData();   Uri photoUri = data.getData();            if (null == photoUri) {                return;            }            //获取照片路径            String[] filePathColumn = {MediaStore.Audio.Media.DATA};            Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);            cursor.moveToFirst();            mPhotoPath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));            cursor.close();
 getContentResolver().query(photoUri, filePathColumn, null, null, null);

返回未空,修改如下,做判断

 if (cursor != null) {                cursor.moveToFirst();                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);                String picturePath = cursor.getString(columnIndex);                cursor.close();                mPhotoPath = picturePath;            } else {                mPhotoPath = photoUri.getPath();            }