错误讯息:
FATAL EXCEPTION: main
Process: com.example.keepair.myapplication, PID: 31796
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66536, result=0, data=null} to activity {com.example.keepair.myapplication/com.example.keepair.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
代码:
mGetImageFromGalleryButton = (Button) view.findViewById(R.id.btn_getgallery);
mGetImageFromGalleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}
else {
startGallery();
}
}
});
private void startGallery() {
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
cameraIntent.setType("image/*");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
if(requestCode == 1000){
Uri returnUri = data.getData();
Bitmap bitmapImage = null;
try {
bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
} catch (IOException e) {
e.printStackTrace();
}
mImageview.setImageBitmap(bitmapImage);
}
returnUri = data.getData();
Glide.with(this)
.load(returnUri)
.override(1280, 1280)
.centerCrop()
.crossFade()
.into(mImageview);
}
else { #What is good for this space? }
}
如果用户点击mGetImageFromGalleryButton
中的GalleryFragment
,我的代码会这样做,用户可以去图库并选择一张图片,然后使用该图片回到GalleryFragment
\ uri
问题:
如果用户单击mGetImageFromGalleryButton
然后转到图库,但不选择任何图片并单击android后退按钮,则上面说的Error message
我发生了。所以,我想我必须修改public void onActivityResult
上的if子句。如果我认为是正确的,那对else { #What is good for this space? }
有什么好处你会帮助我吗?
答案 0 :(得分:2)
tintColor
答案 1 :(得分:1)
如果不选择任何图像,则数据为空。这就是它给出错误的原因。如果您的数据不为空,请在代码周围添加条件。
E.g
if(data != null){
//Proceed with your image crop etc
}