为什么我应该通过Picasso库下载图像而不是仅使用此代码:
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try
{
in = OpenHttpGETConnection(URL);
bitmap = BitmapFactory.decodeStream(in); in.close();
}
catch (Exception e)
{
Log.d("DownloadImage", e.getLocalizedMessage());
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
另一个问题:
毕加索是在UI中还是通过后台线程下载图像?
我在我的项目中使用Picasso库来加载图像和缓存它们.它没有任何问题,效果很好.但是,当我尝试使用OkHttp库与我的服务器(JSON通信)进行数据通信时,Picasso会抛出异常.
我使用以下罐子:okhttp-2.0.0-RC2,okio-1.0.0,picasso-2.2.0.当我添加这些罐子后运行我的项目时,它崩溃了以下内容:
06-12 11:13:15.824: E/dalvikvm(12105): Could not find class 'com.squareup.okhttp.HttpResponseCache', referenced from method com.squareup.picasso.OkHttpDownloader.<init>
Run Code Online (Sandbox Code Playgroud)
我添加了okhttp只是为了使用以下方法:
public static String executeHttpGet(String urlStr) {
Response response = null;
String result = "";
OkHttpClient client = new OkHttpClient();
try {
Request request = new Request.Builder().url(urlStr).build();
response = client.newCall(request).execute();
result = response.body().string();
} catch (Exception ex) {
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有任何问题.然而,使用Picasso库并且用于完美工作的代码开始抛出以下例外:
06-12 11:19:49.307: E/AndroidRuntime(13036): FATAL EXCEPTION: main
06-12 11:19:49.307: E/AndroidRuntime(13036): java.lang.NoClassDefFoundError: com.squareup.okhttp.HttpResponseCache
06-12 11:19:49.307: E/AndroidRuntime(13036): at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:74)
06-12 11:19:49.307: E/AndroidRuntime(13036): at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:51)
06-12 11:19:49.307: …Run Code Online (Sandbox Code Playgroud) 我正在使用Picasso库版本2.71828加载一些图像,但是它不适用于所有URL。这是我的代码:
Picasso.get().load(url).into(imageView);
Run Code Online (Sandbox Code Playgroud)
url1:https : //res.cloudinary.com/lastminute/image/upload/c_scale,w_630/v1431701424/52347407_Casino_Tower_2100x1400_pyzvxz.jpg
url2:http : //images.foody.vn/res/g14/138986/prof/s576x330/foody-mobile-a2-jpg-261-635682356468932282.jpg
url3:https : //static3.mytour.vn/resources/pictures/hotels/19/large_vlj1419841660_khach-san-gia-han.JPG
毕加索仅适用于url1和url2。url3即使我可以在浏览器中打开它,它也不会显示图像。
为什么可以加载url3Picasso?毕加索无法加载哪些类型的网址?