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

时间:2018-06-12 07:11:51

标签: java android nullpointerexception compression android-bitmap

在我的应用程序中,我想从图库压缩中选择图像。
对于压缩,我使用此库:https://github.com/zetbaitsu/Compressor

我为它编写了下面的代码,但是当我运行应用程序并从库中选择图像时,它会抛出强制关闭并在 logCat 中显示以下错误:

我的活动代码:

    @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);

更新:仅针对某些图片显示此错误并非所有图片! 我该如何解决?请帮帮我

3 个答案:

答案 0 :(得分:1)

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();
                }
            }

        }
}

答案 1 :(得分:1)

请通过try和catch块处理并向用户显示对话框消息以选择其他图像,可能会选择图像已损坏。

答案 2 :(得分:0)

确保该文件不为空。

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();
                }
            }

        }
}
相关问题