您的位置:首页 > 其它

PhotoView无法显示大图片问题

2017-05-20 12:56 375 查看
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方法还是有其他方式可以进行改善,欢迎大家提供一个更好的方式,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 imageview oom