你好,我是一名Android新手。我想从URL中将图像插入到imageView中,但每次加载imageView时,第二次都应该在没有互联网的情况下插入,也就是说它应该被存储在缓存中。
你好,我是一名Android新手。我想从URL中将图像插入到imageView中,但每次加载imageView时,第二次都应该在没有互联网的情况下插入,也就是说它应该被存储在缓存中。
你可以使用Picasso库来实现此功能。你可以从Picasso库网站下载它,然后将这个jar文件放入libs文件夹中。它将管理图片的所有属性。
http://square.github.io/picasso/String imgURL = "Your URL";
ivmage = (ImageView) findViewById(R.id.imageView1);
Picasso.with(MainActivity.this).load(imgURL).into(ivmage);
Picasso
库。Picasso
,有以下优点:
Url
加载 Image
。Picasso.with(context).load(url).into(imageView);
请参考此链接获取API:Picasso
您可以使用 Glide https://github.com/bumptech/glide
Glide.with(this).load("imageURl").into(imageView);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
mImageView.setImageResource(R.drawable.image_load_error);
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);
如果您正在使用Volley,则必须手动缓存图像。 Picasso会默认缓存图像。