猿问

android真机测试时获取系统图片的Cursor老是空指针异常,怎么解?

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;

}

}

}

57592be40001373a05000152.jpg


慕九州4544299
浏览 1912回答 1
1回答

Genment

希望能多提供一点代码,以及相关log。
随时随地看视频慕课网APP

相关分类

Android
我要回答