首页 > 解决方案 > 调整大小并保存图像

问题描述

我正在使用 Glide 4.7.1 尝试调整大小并保存一些照片。

for (String path : this.paths) {
        Glide.with(this.context).asBitmap().load(path).apply(RequestOptions.fitCenterTransform()).into(new SimpleTarget<Bitmap>(1080, 1920) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                try {
                    resource.compress(Bitmap.CompressFormat.JPEG, 85, new FileOutputStream(MasterFacade.getAppDir() + "temp/" + resource.hashCode() + ".jpg"));
                    listener.onUpdate(progressMessage, processed.get());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

我只想将大小(保持纵横比)调整为 1080x1920(或最接近的),然后将新位图保存到文件中。

我确定我有权写我确定path图像存在我确定MasterFacade.getAppDir() + "temp/" + resource.hashCode() + ".jpg"是一个有效的目录并且我已经在上面写了权限

但是这个代码永远达不到!

我尝试调试,但从onResourceReady未被调用...

我究竟做错了什么?

标签: androidimagebitmapandroid-glide

解决方案


在 Glide 4.x 中使用此调整图像大小

Glide.with(getContext())
.load(imagePath)
.apply(new RequestOptions().override(600, 200))
.fitCenter() 
.into(setImage);

推荐阅读