OnActivity结果有时在ACTION_GET_CONTENT意图后不调用

我正在开发一个图像编辑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。onPauseonActivityResultonActivityResult

问题:为什么有时被叫,有时不叫?是否有任何其他可能的原因导致为 null?onActivityResultphotoBitmap


答案 1

您必须添加以下代码才能从设备拍摄图像

choosebtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent i1=new Intent();
         i1.setType("image/*");
         i1.setAction(Intent.ACTION_GET_CONTENT);
         startActivityForResult(i1,1);
      }
});


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null)
    {
        imguri=data.getData();
        mainpreview.setImageURI(data.getData());
    } else {
        Toast.makeText(getApplicationContext(),"please choose image",Toast.LENGTH_SHORT).show();
    }
}

答案 2

试试这个,它工作完美

     Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, LOAD_IMAGE_RESULTS);

  @SuppressLint("Recycle")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

           if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
                    // Let's read picked image data - its URI
                    Uri pickedImage = data.getData();

                    // Let's read picked image path using content resolver
                    String[] filePath = {MediaStore.Images.Media.DATA};
                    @SuppressLint("Recycle") Cursor cursor = null;
                    if (pickedImage != null) {
                        cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
                    }
                    if (cursor != null) {
                        cursor.moveToFirst();

                        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
      }
    }
}

现在您可以转换为位图imagepath


推荐