Android 使用毕加索从URL保存图像而不改变大小(使用bitmap.compress()更改大小)
我正在使用毕加索进行图像处理,并使用它从后端服务器下载图像并保存到本地设备。我使用Android 使用毕加索从URL保存图像而不改变大小(使用bitmap.compress()更改大小),android,image-processing,picasso,Android,Image Processing,Picasso,我正在使用毕加索进行图像处理,并使用它从后端服务器下载图像并保存到本地设备。我使用Target保存图像 Picasso.with(context) .load(url) .into(target); 由于目标代码获得位图,我必须使用bitmap.compress()将图像写入本地磁盘,并且我使用质量为100的JPEF格式,假设这将保持原始质量 读起来好像这不是我想要的。在一个例子中,后端的映像是90kb,写
Target
保存图像
Picasso.with(context)
.load(url)
.into(target);
由于目标代码获得位图,我必须使用bitmap.compress()
将图像写入本地磁盘,并且我使用质量为100的JPEF格式,假设这将保持原始质量
读起来好像这不是我想要的。在一个例子中,后端的映像是90kb,写入的映像是370kb。原始图像可以使用任意质量值生成。使用毕加索保存图像而不改变大小/质量的最简单方法是什么
Target target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom arg1) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
FileOutputStream out = null;
try {
out = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void info) {
}
}.execute();
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
};
Target Target=新目标(){
@凌驾
准备加载时的公共无效(可提取arg0){
}
@凌驾
已加载BitMapLoaded(最终位图,Picasso.LoadedFrom arg1){
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
FileOutputStream out=null;
试一试{
out=新文件输出流(imagePath);
bitmap.compress(bitmap.CompressFormat.JPEG,100,out);
out.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
受保护的void onPostExecute(void info){
}
}.execute();
}
@凌驾
公共void onBitmapFailed(可提取arg0){
}
};
更新:这里有更优雅的解决方案:
使用此解决方案解决了问题
在我的PagerAdapter
的InstanceItem()
方法中,我运行了一个AsyncTask
,它将下载图像,保存到一个文件中,然后用该图像文件调用毕加索
PagerAdapter实例化项()
RemoteImageToImageViewAsyncTask
public class RemoteImageToImageViewAsyncTask extends AsyncTask<Void, Void, Void> {
ImageView imageView;
String url;
File output;
public RemoteImageToImageViewAsyncTask(ImageView imageView, String url, File output) {
this.imageView = imageView;
this.url = url;
this.output = output;
}
@Override
protected Void doInBackground(Void... params) {
// Downloads the image from url to output
ImageDownloader.download(url, output);
return null;
}
@Override
protected void onPostExecute(Void params) {
Picasso.with(context)
.load(output)
.into(imageView);
}
}
公共类RemoteImageToImageViewAsyncTask扩展了AsyncTask{
图像视图图像视图;
字符串url;
文件输出;
公共RemoteImageToImageViewAsyncTask(ImageView、字符串url、文件输出){
this.imageView=imageView;
this.url=url;
这个。输出=输出;
}
@凌驾
受保护的Void doInBackground(Void…参数){
//将图像从url下载到输出
ImageDownloader.download(url,输出);
返回null;
}
@凌驾
受保护的void onPostExecute(void参数){
毕加索。与(上下文)
.负载(输出)
.进入(图像视图);
}
}
public class RemoteImageToImageViewAsyncTask extends AsyncTask<Void, Void, Void> {
ImageView imageView;
String url;
File output;
public RemoteImageToImageViewAsyncTask(ImageView imageView, String url, File output) {
this.imageView = imageView;
this.url = url;
this.output = output;
}
@Override
protected Void doInBackground(Void... params) {
// Downloads the image from url to output
ImageDownloader.download(url, output);
return null;
}
@Override
protected void onPostExecute(Void params) {
Picasso.with(context)
.load(output)
.into(imageView);
}
}