OnActivity结果有时在ACTION_GET_CONTENT意图后不调用
2022-09-01 04:01:14
我正在开发一个图像编辑Android应用程序。在我的一个活动中,我称之为从onCreate()中的图库中选择图像的意图,如下所示:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
然后我收到这样的数据:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Crashlytics.log("onActivityResult called");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Crashlytics.log("Data received from image pick intent");
imageUri = data.getData();
loadImage();
} else {
//if we do not select a picture, go back to the dashboard
Crashlytics.log("Data not received");
onBackPressed();
Log.d(TAG, "no picture selected");
}
}
方法:loadImage
private void loadImage() {
try {
photoBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
Crashlytics.log("IOException from getBitmap");
Log.d(TAG, e.getMessage());
showToastAndPressBack();
return;
}
if (photoBitmap == null) {
Crashlytics.log("photoBitmap is null in onActivityResult");
showToastAndPressBack();
return;
}
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
imgVWidth = size.x;
int height = size.y;
imgVHeight = (int) (((float) imgVWidth / photoBitmap.getWidth()) * photoBitmap.getHeight());
photoInImgViewBitmap = Bitmap.createScaledBitmap(photoBitmap, imgVWidth, imgVHeight, true);
imageAlreadyPicked = true;
}
现在我的问题是,有时我在Crashlytics中看到NPE-s声称当用户按下下一个按钮时photoBitmap为空。
@OnClick(R.id.toolbar_next)
void onToolbarNextClick() {
float originalScale = (float) (previewImageView.getHeight()) / (float) (photoBitmap.getHeight());
...
}
我看到的唯一 Crashlytics 日志是用户出于意图而离开(我在里面放置了 Crashlytics 日志)。没有 log for ,所以我最好的猜测是没有调用,因此我的位图未加载,因此当用户按下 next 时它将为 null。onPause
onActivityResult
onActivityResult
问题:为什么有时被叫,有时不叫?是否有任何其他可能的原因导致为 null?onActivityResult
photoBitmap