我的应用程序从Marshmallow设备中的图库中获取图像,但是当我在API级别为18的设备上运行它时,我得到了异常。
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity
这是获取权限的代码;
private static final int SELECT_IMAGE = 1;
public void addImage(View view){
if (Build.VERSION.SDK_INT < 23){
ImagePicker();
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
ImagePicker();
}
}
}
这是获取图像的意图;
public void ImagePicker (){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("return-data", true);
startActivityForResult(intent, SELECT_IMAGE);
}
以下是获取结果的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
profileImage = (ImageView) findViewById(R.id.profilePicture);
if (resultCode != Activity.RESULT_CANCELED ) {
if (requestCode == SELECT_IMAGE && data != null) {
Bundle extra = data.getExtras();
Bitmap image = extra.getParcable("data");
profileImage.setImageBitmap(image);
}
}
请帮助,谢谢。