android真机测试时获取系统图片的Cursor老是空指针异常,怎么解?手机用的是米4,android 6.0。可以打开图库,选择图片后返回出现空指针异常,cursor为空。Cursor cursor = act.getContentResolver().query(uri, projection, null, null, null);uri和projection都有值,模拟器测试正常。
public void changePic(View view){
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
String fileName = null;
if(requestCode==1 && resultCode==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();//空指针异常,selectedImage, filePathColumn两个参数都有值
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
String picturePath=cursor.getString(columnIndex);
cursor.close();
PictrueConfig pic=new PictrueConfig();
Bitmap bitmap=pic.compressImageFromFile(fileName);
Date date = new Date(System.currentTimeMillis());
String dateTime = date.getTime() + "";
shopPic="pic"+dateTime+"_11.jpg";
fileName=pic.saveToSdCard(bitmap, dateTime, ShopManaActivity.this);
shopImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
targeturl = fileName;
}
}
}
Genment
相关分类