Search code examples
androidthisfile-comparison

How to Fill the 'this' Context in Compressor Library in Android Studio


I am trying to write a code to compress an image using Compressor library: https://github.com/zetbaitsu/Compressor and use this line to do the compression:

compressedImageFile = new Compressor(this).compressToFile(actualImageFile);

However, when building my application, I got this error message:

error: incompatible types: PictureOneFragment cannot be converted to Context

I changed 'this' to getActivity(). I got another error message:

error: unreported exception IOException; must be caught or declared to be thrown

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PICTURE_CAPTURE && resultCode == Activity.RESULT_OK) {

            File imgFile = new  File(pictureFilePath);

            if(imgFile.exists()) {
                File compressedImageFile = new Compressor(getActivity()).compressToFile(imgFile);

                try {

                    mPage.getData().putString(PictureOnePage.PICTURE_NAME + "_" + mPage.getTitle().trim(), pictureFilePath);
                    mPage.notifyDataChanged();

                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(imgFile));
                    mImgView.setImageBitmap(bitmap);

                } catch (IOException e) {
                    Log.i("eProject : ", e.toString());
                    e.printStackTrace();
                }

            }

        }
    }

What is the right context for Compressor library?


Solution

  • just little change is there, you have to create file instance and cropImage instance

    in between try{
    .... } catch(IOException e){ ... }
    ,because both can throw IOException at run time.