【问题标题】:Bitmap is null while capturing image from camera从相机捕获图像时位图为空
【发布时间】:2019-04-19 01:20:55
【问题描述】:

我的代码在 API 级别

public void SelectImage() {
        final CharSequence[] items = {"Camera", "Gallery", "Cancel"};

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.add_image);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                if (items[i].equals("Camera")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (intent.resolveActivity(mainActivity.getPackageManager()) != null) {
                        startActivityForResult(intent, REQUEST_CAMERA);
                    }
                } else if (items[i].equals("Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(intent, SELECT_FILE);

                } else if (items[i].equals("Cancel")) {
                    dialog.dismiss();
                }

            }
        });


public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                place = PlacePicker.getPlace(data, mainActivity);
                etAddress1.setText(place.getAddress());
            }
        } else if (resultCode == Activity.RESULT_OK && data != null) {

            Glide.with(this).load(data.getData()).into(ivprofile);
            Bitmap bm = null;
            try {
                bm = MediaStore.Images.Media.getBitmap(mainActivity.getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            byte[] b = baos.toByteArray();
            base64img = Base64.encodeToString(b, Base64.NO_WRAP);
        }
    }

这里我的 bm 为空,为什么会发生这种情况,我使用的是旧代码版本还是还有其他什么......提前致谢。

【问题讨论】:

    标签: java android bitmap android-bitmap


    【解决方案1】:

    实际上,有一个库适用于每部手机和每个 android 版本。如果你需要解决你的问题,这里有一个答案。 EasyImage 成功了!

    不要忘记处理 api 23 及更高版本的权限!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多