如何修复位图上 getWidth() 中的空错误

How to fix null error in getWidth() on bitmap

在我的应用程序中,我想 select 来自 画廊 的图像并 压缩 它。
对于压缩,我使用这个库:https://github.com/zetbaitsu/Compressor

我为它写了下面的代码,但是当我 运行 应用程序和来自图库的 select 图片时,它抛出 force close 并显示下面的错误在 logCat 中:

我的activity代码:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) {
                if (data != null) {
                    // Get the Image from data
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    assert cursor != null;
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    mediaPath = cursor.getString(columnIndex);
                    cursor.close();

                    postPath = mediaPath;
                    uploadImageToServer(postPath);
                }

            }
        } else if (resultCode != RESULT_CANCELED) {
        }
    }
}
    private void uploadImageToServer(String imagePath) {
        if (imagePath == null || imagePath.equals("")) {
            return;
        } else {
            showpDialog();
            Map<String, RequestBody> map = new HashMap<>();
            File file = new File(imagePath);
            try {
                compressedImage = new Compressor(getActivity())
                        .setMaxWidth(2000)
                        .setMaxHeight(1400)
                        .setQuality(90)
                        .setCompressFormat(Bitmap.CompressFormat.JPEG)
                        .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                                Environment.DIRECTORY_PICTURES).getAbsolutePath())
                        .compressToFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
}

LogCat 错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                                       at id.zelory.compressor.ImageUtil.decodeSampledBitmapFromFile(ImageUtil.java:70)
                                                                       at id.zelory.compressor.ImageUtil.compressImage(ImageUtil.java:33)
                                                                       at id.zelory.compressor.Compressor.compressToFile(Compressor.java:60)
                                                                       at id.zelory.compressor.Compressor.compressToFile(Compressor.java:56)
                                                                       at com.app.android.Fragments.NewAddressFragment.uploadImageToServer(NewAddressFragment.java:486)
                                                                       at com.app.android.Fragments.NewAddressFragment.onActivityResult(NewAddressFragment.java:353)
                                                                       at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
                                                                       at android.app.Activity.dispatchActivityResult(Activity.java:7295)

显示此行的错误: .compressToFile(file);

更新: 仅针对部分图像而非所有图像显示此错误! 我该如何解决?请帮助我

只需确保该文件不为空。

private void uploadImageToServer(String imagePath) {
        if (imagePath == null || imagePath.equals("")) {
            return;
        } else {
            showpDialog();
            Map<String, RequestBody> map = new HashMap<>();
            File file = new File(imagePath);

            if(file != null && file.exists() && file.canRead()) {
                try {
                    compressedImage = new Compressor(getActivity())
                            .setMaxWidth(2000)
                            .setMaxHeight(1400)
                            .setQuality(90)
                            .setCompressFormat(Bitmap.CompressFormat.JPEG)
                            .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES).getAbsolutePath())
                            .compressToFile(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
}

GitHub 的另一个假设是,您可能尝试将更改后的文件以原始名称存储在原始路径中。这也可能导致问题。请尝试此版本的代码。我添加了 compressedFileName.

private void uploadImageToServer(String imagePath) {
        if (imagePath == null || imagePath.equals("")) {
            return;
        } else {
            showpDialog();
            Map<String, RequestBody> map = new HashMap<>();
            File file = new File(imagePath);

            if(file != null && file.exists() && file.canRead()) {
                try {
                    String compressedFileName = "_" + file.getName();

                    compressedImage = new Compressor(getActivity())
                            .setMaxWidth(2000)
                            .setMaxHeight(1400)
                            .setQuality(90)
                            .setCompressFormat(Bitmap.CompressFormat.JPEG)
                            .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES).getAbsolutePath())
                            .compressToFile(file, compressedFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
}

请处理 try 和 catch 块并向用户显示 select 另一张图片的对话框消息,可能 selected 图片已损坏。