如何在 Android Studio 的 Compressor 库中填充 'this' 上下文

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

我正在尝试编写代码以使用 Compressor 库压缩图像:https://github.com/zetbaitsu/Compressor 并使用此行进行压缩:

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

但是,在构建我的应用程序时,我收到以下错误消息:

error: incompatible types: PictureOneFragment cannot be converted to Context

我将 'this' 更改为 getActivity()。我收到另一条错误消息:

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

            }

        }
    }

Compressor 库的正确上下文是什么?

只有一点点变化,您必须创建 文件实例cropImage 实例

介于两者之间 尝试{
.... } 赶上(IOException e){ ... }
,因为两者都可以在 运行 时间抛出 IOException。

直接剪线:

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

并将它放在它下面的 try 块中。
就这么简单,因为它也会导致 IOException.