【问题标题】:How to Fill the 'this' Context in Compressor Library in Android Studio如何在 Android Studio 的 Compressor 库中填充“this”上下文
【发布时间】:2019-05-10 09:43:00
【问题描述】:

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

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

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

错误:不兼容的类型:PictureOneFragment 无法转换为 Context

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

错误:未报告的异常IOException;必须被抓住或宣布被扔掉

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 库的正确上下文是什么?

【问题讨论】:

    标签: android this file-comparison


    【解决方案1】:

    只是变化不大,你必须创建 file instancecropImage instance

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

    【讨论】:

      【解决方案2】:

      切线:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-31
        • 2016-01-27
        • 2023-04-09
        • 2020-07-13
        • 2023-03-24
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多