Android 从Picasso加载的ImageView获取位图
我有一个加载图像的方法,如果图像在服务器上查找之前没有加载。然后将其存储在应用程序文件系统中。如果它在文件系统中,它会加载该映像,因为这比从服务器中提取映像要快得多。如果您以前加载过图像,但未关闭应用程序,则图像将存储在静态词典中,以便在不使用更多内存的情况下重新加载图像,以避免出现内存不足错误 在我开始使用毕加索图像加载库之前,一切都很顺利。现在我正在将图像加载到ImageView中,但我不知道如何获取返回的位图,以便将其存储在文件或静态字典中。这使事情变得更加困难。因为这意味着它每次都试图从服务器加载映像,这是我不希望发生的事情。在将位图加载到ImageView后,是否有办法获取位图?下面是我的代码:Android 从Picasso加载的ImageView获取位图,android,bitmap,imageview,picasso,Android,Bitmap,Imageview,Picasso,我有一个加载图像的方法,如果图像在服务器上查找之前没有加载。然后将其存储在应用程序文件系统中。如果它在文件系统中,它会加载该映像,因为这比从服务器中提取映像要快得多。如果您以前加载过图像,但未关闭应用程序,则图像将存储在静态词典中,以便在不使用更多内存的情况下重新加载图像,以避免出现内存不足错误 在我开始使用毕加索图像加载库之前,一切都很顺利。现在我正在将图像加载到ImageView中,但我不知道如何获取返回的位图,以便将其存储在文件或静态字典中。这使事情变得更加困难。因为这意味着它每次都试图从
public Drawable loadImageFromWebOperations(String url,
final String imagePath, ImageView theView, Picasso picasso) {
try {
if (Global.couponBitmaps.get(imagePath) != null) {
scaledHeight = Global.couponBitmaps.get(imagePath).getHeight();
return new BitmapDrawable(getResources(),
Global.couponBitmaps.get(imagePath));
}
File f = new File(getBaseContext().getFilesDir().getPath()
.toString()
+ "/" + imagePath + ".png");
if (f.exists()) {
picasso.load(f).into(theView);
下面这行是我试图检索位图时抛出的空指针异常,我想这是因为毕加索需要一段时间才能将图像添加到ImageView
Bitmap bitmap = ((BitmapDrawable)theView.getDrawable()).getBitmap();
Global.couponBitmaps.put(imagePath, bitmap);
return null;
} else {
picasso.load(url).into(theView);
return null;
}
} catch (OutOfMemoryError e) {
Log.d("Error", "Out of Memory Exception");
e.printStackTrace();
return getResources().getDrawable(R.drawable.default1);
} catch (NullPointerException e) {
Log.d("Error", "Null Pointer Exception");
e.printStackTrace();
return getResources().getDrawable(R.drawable.default1);
}
}
任何帮助都将不胜感激,谢谢 毕加索让您可以直接控制下载的图像。要将下载的图像保存到文件中,请执行以下操作:
Picasso.with(this)
.load(currentUrl)
.into(saveFileTarget);
其中:
saveFileTarget = new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + FILEPATH);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
既然你用的是毕加索,你也不妨充分利用它。它包括一个强大的缓存机制 在
picasso
实例上使用picasso.setDebugging(true)
查看发生的缓存类型(图像从磁盘、内存或网络加载)。请参阅:(调试指示器)
毕加索的默认实例配置为()
LRU内存缓存为可用应用程序RAM的15%
2%存储空间的磁盘缓存,最高50MB,但不低于5MB。(注意:这仅适用于API 14+或使用在所有API级别(如OkHttp)上提供磁盘缓存的独立库时)
您还可以使用
Picasso.Builder
来指定缓存
实例以自定义实例,还可以指定下载程序
,以实现更精细的控制。(毕加索有一个OkDownloader
的实现,如果你的应用程序中包含OkHttp
,它会自动使用。)毕加索不需要实现你自己的静态字典,因为它是自动为你实现的@intrepidkarthi的观点是正确的,即图像在第一次加载时由毕加索自动缓存,因此它不会不断调用服务器来获取相同的图像
也就是说,我发现自己处于类似的情况:我需要访问下载的位图,以便将其存储在应用程序中的其他位置。为此,我稍微修改了@Gilad Haimov的答案:
Java,使用毕加索的旧版本:
Picasso.with(this)
.load(url)
.into(new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from) {
/* Save the bitmap or do something with it here */
// Set it in the ImageView
theView.setImageBitmap(bitmap);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
});
Picasso.get()
.load(url)
.into(object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
/* Save the bitmap or do something with it here */
// Set it in the ImageView
theView.setImageBitmap(bitmap)
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}
})
Kotlin,毕加索版本2.71828:
Picasso.with(this)
.load(url)
.into(new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from) {
/* Save the bitmap or do something with it here */
// Set it in the ImageView
theView.setImageBitmap(bitmap);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
});
Picasso.get()
.load(url)
.into(object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
/* Save the bitmap or do something with it here */
// Set it in the ImageView
theView.setImageBitmap(bitmap)
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}
})
这使您可以在异步加载位图的同时访问加载的位图。您只需记住在ImageView中设置它,因为它不再自动为您完成
另一方面,如果你想知道你的图像来自哪里,你可以用同样的方法访问这些信息。上述方法的第二个参数,Picasso.LoadedFrom
,是一个枚举,它让您知道位图是从哪个源加载的(三个源分别是磁盘
、内存
和网络
).Square Inc.还提供了一种直观的方式,通过使用所述的调试指示器来查看位图的加载位置
希望这有帮助!使用这个
Picasso.with(context)
.load(url)
.into(imageView new Callback() {
@Override
public void onSuccess() {
//use your bitmap or something
}
@Override
public void onError() {
}
});
希望它能帮助您在本例中,我用来设置colapsing工具栏的背景
public class MainActivity extends AppCompatActivity {
CollapsingToolbarLayout colapsingToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
colapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.colapsingToolbar);
ImageView imageView = new ImageView(this);
String url ="www.yourimageurl.com"
Picasso.with(this)
.load(url)
.resize(80, 80)
.centerCrop()
.into(image);
colapsingToolbar.setBackground(imageView.getDrawable());
}
我希望它对您有所帮助。Picasso不会每次都从服务器加载图像。只有在第一次从缓存目录加载图像时才会这样做。这也可以通过Drawable完成吗?(我正在尝试将tp追加到EditText中)我没有收到任何错误,但它不起作用
onSuccess()
不提供位图对象。onPrepareLoad onBitmapFailed中的问号保存了我的日期。我无法覆盖目标的方法,我收到错误“此类型为最终类型,因此无法从中继承”…你使用的是什么版本的毕加索?看起来它可能在最新的资料中被重命名为BitmapTarget
。仅供参考,以防有人弄错我。请确保你像这样从com.square.Picasso选择“Target”接口。否则你将找不到onBitmapLoaded方法。上面的kotlin代码可以工作我尝试了相同的方法,但图像没有加载到我的recycleview中。imageView.getDrawable()
为空