先上代码
Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, IMAGE);
然后在onActivityResult方法中
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && null != data) {//媒体库返回
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}
现在有一个问题,我在一台三星手机上发送图片是可以实现的,cursor对象不为null
但现在换了一台小米手机,取图片的时候就报错了,cursor为null。
报错信息就是到 cursor.moveToFirst();报空指针异常