-1

In my application i want to select image from gallery and compress it.
For compress i use this library : https://github.com/zetbaitsu/Compressor

I wrote below codes for it, but when i run the application and select image from gallery, it throws force close and show below errors in logCat :

My activity codes :

    @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 error :

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)

Show error for this line : .compressToFile(file);

UPDATE : show this error just for some images no all images! How can i fix it? please help me

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hock
  • 147
  • 2
  • 2
  • 14

3 Answers3

1

Another assumption on GitHub is, that you probably try to store the changed file with the original name in the original path. This also may lead to problems. Please try this version of code. I added 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

Please handle through try and catch block and show dialog message to user for select another image, may be selected image is corrupted.

Hkh
  • 357
  • 1
  • 10
0

Just make sure that file is not null.

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

        }
}
  • again show me error :( – Hock Jun 12 '18 at 07:23
  • Sorry, my bad. This was still only a path check. I just extended the if-clause. It now checks whether the file itself exists. Please try again. –  Jun 12 '18 at 07:26
  • not work again! why show me error! i'm bad lucky – Hock Jun 12 '18 at 07:32
  • Still the same error? Or some other kind of error? –  Jun 12 '18 at 07:33
  • show above error again! – Hock Jun 12 '18 at 07:36
  • Okay, maybe it really has to do with the accessibility of the file. I extended the if-clause again. It now checks additionally `file.canRead()`. This should be sufficient for your needs. There are also `file.canWrite()` and `file.canExecute()` but I reckon you won't neither write into the file nor execute it. ;) -- try again, please. –  Jun 12 '18 at 07:41
  • not work again :( . can you see this link : https://github.com/zetbaitsu/Compressor/issues/49 please see this link and help me. please :( – Hock Jun 12 '18 at 07:54
  • can you help me my bro? please. i really need your help. please – Hock Jun 12 '18 at 07:59
  • are you here my bro? :( – Hock Jun 12 '18 at 08:08
  • I posted another answer. Give it a try. –  Jun 12 '18 at 08:13