我刚刚在我的项目中导入了Picasso库,我正在使用它,就像在另一个SO question中所描述的那样(我需要从Web服务器下载并保存一些图像)。除了一件事之外,整个过程正常工作:我的模拟器中生成的图像比服务器上的原始图像更大(就重量而言)。例如,52Kb(370×505像素)的JPG图像导致相同分辨率(370×505)但重量为132Kb的JPG图像。这是我正在使用的代码(在很多SO问题中看到过):
if (target == null){
target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(getActivity().getFilesDir() + "/saved.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
}
Picasso.with(getActivity()).load(sourceValue).into(target);
提前感谢大家的时间和关注。
更新
检查我的模拟器的内存,我注意到毕加索创建的图像的缓存版本具有正确的大小。所以,基本上,如果我使用相同的代码我发布,但我评论所有的try-catch块,文件只是下载和缓存(不保存在其他位置),但具有正确的大小。那么这是压缩的错误吗?