PhotoView无法显示大图片问题

本文介绍了解决在移动应用中加载和显示大尺寸图片的问题。通过调整图片分辨率及使用SubsamplingScaleImageView替代PhotoView,实现了高效且清晰的大图片预览功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PhotoView无法显示大图片,这些日子做的聊天IM程序,需要预览图片,一开始使用的是PhotoView,没有任何毛病,可是当对方发过来一张长截图的时候却显示不了了,自己很烦,自己又去自定义了一个ImageView,可是效果依然是一样;而且Bitmap还老是报出OOM,没办法,我只能先判断图片的大小、判断图片分辨率,如果超过了480*800*3就将图片的宽度高度设置为原来的二分之一,好了问题完美解决;以下是代码:

Bitmap bitmap;
if (size <= 480 * 800 * 3) {       //ARGB_8888 表示32位的ARGB位图占用4个字节
    bitmap = BitmapFactory.decodeFile(cachePath);
} else {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一
    bitmap = BitmapFactory.decodeFile(cachePath, options);
}
pv.setImageBitmap(bitmap);
虽然问题解决,可是总还是感觉不爽,因为这样有的图片的清晰度还是不够,分辨率不够,没办法,最后只能放弃使用PhotoView了,而是使用了
SubsamplingScaleImageView这个控件,大家可以上网找;
不过该控件不能直接将url作为资源将图片显示出来,而是要先将图片下载到本地,然后再通过本地路径再去将图片放到控件当中
以下是代码:
private void downloadImageAndShow(String msgId,String path, SubsamplingScaleImageView bivPic){
    //下载图片并显示
    String destFileDir = Environment.getExternalStorageDirectory() + "/hz_tencent_clound_images";
    File dir = new File(destFileDir);
    final File file = new File(destFileDir, msgId + "." + path.substring(path.lastIndexOf(".") + 1));
    Log.e("fileUrl", path);
    if (!dir.exists()) {        //路径不存在,创建并下载图片
        dir.mkdirs();
        //TODO 下载图片并显示
        downloadImageAndShow(bivPic,file,path);
    } else {        //路径存在,直接下载图片
        if (file.exists()) {      //路径存在包括可能说明文件也存在,直接显示
            showImage(bivPic,file.getPath());
        }else{
            //TODO 下载图片并显示
            downloadImageAndShow(bivPic,file, path);
        }
    }
}

private Handler mHandler = new Handler();

private void downloadImageAndShow(final SubsamplingScaleImageView bivPic,File file,String fileUrl){
    SystemApi systemApi = new SystemApi();
    systemApi.downLoadSoundFile(file, fileUrl, new DownLoadSoundCallBack() {
        @Override
        public void errorCallBack(Call call, final Exception e) {
            Runnable uiRunnable = new Runnable() {
                @Override
                public void run() {
                    Log.e(TAG, e.getMessage());
                }
            };
            mHandler.post(uiRunnable);
        }

        @Override
        public void succCallBack(final String path) {
            Runnable uiRunnable = new Runnable() {
                @Override
                public void run() {
                    showImage(bivPic,path);
                }
            };
            mHandler.post(uiRunnable);
        }
    });
}

private void showImage(SubsamplingScaleImageView imageView,String path){
    imageView.setImage(ImageSource.uri(path));
}
其中downLoadSoundFile方法的代码如下:
public void downLoadSoundFile(final File file, String fileUrl, final DownLoadSoundCallBack callBack) {
    OkHttpClient okHttpClient = new OkHttpClient();
    final Request request = new Request.Builder().url(fileUrl).build();
    final Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            callBack.errorCallBack(call, e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            InputStream is = null;
            byte[] buf = new byte[2048];
            int len = 0;
            FileOutputStream fos = null;
            try {
                is = response.body().byteStream();
                fos = new FileOutputStream(file);
                while ((len = is.read(buf)) != -1) {
                    current += len;
                    fos.write(buf, 0, len);
                }
                fos.flush();
            } catch (IOException e) {
                callBack.errorCallBack(null, e);
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    callBack.succCallBack(file.getPath());
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                }
            }
        }
    });
}
个人觉得downLoadSoundFile方法还是有其他方式可以进行改善,欢迎大家提供一个更好的方式,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TenTenXu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值