【发布时间】:2018-09-06 17:05:21
【问题描述】:
尝试在 UI 上显示来自自动下载链接的图片时遇到问题。 Picasso 和 Glide 不知道如何打开它。他们正在寻找地址。
例如,我有这张图片https://graph.facebook.com/10155174010491889/picture?type=large。当您访问该链接时,您会自动获得下载的图片。毕加索不知道如何处理这个问题。
我研究了这个问题(https://github.com/square/picasso/issues/463),应用了 OkHttpClient 和下载器,但还是不行:
OkHttpClient client = new OkHttpClient.Builder().build();
Downloader downloader = new OkHttp3Downloader(client);
new Picasso.Builder(context)
.downloader(downloader)
.listener((picasso, uri, exception) -> {
exception.printStackTrace();
Log.d(TAG, "loadImageWithPicasso: " + exception);
})
.build()
.load(Uri.parse(path))
.centerCrop()
.fit()
.into(imageView, new Callback() {
@Override
public void onSuccess() {
imageView.setImageDrawable(ImageUtils.roundedCornersImage(context, imageView));
}
@Override
public void onError() {
imageView.setImageResource(resourceDrawable);
}
});
我什至单独尝试对该图像发出 OkHTTP 请求并得到奇怪的结果 - 位图为空(尝试下载为位图时),网络响应重定向 url 大小为 0 的图像 (https://m.facebook.com/platform/profilepic/?asid=10155174010491889&height=200&width=200)
getRequest("https://graph.facebook.com/10155174010491889/picture?type=large", new okhttp3.Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "body = " + response.body());
Log.d(TAG, "network response = " + response.networkResponse().toString());
InputStream inputStream = response.body().byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Log.d(TAG, response.networkResponse().request().url() + " <<<<");
}
@Override
public void onFailure(Call call, IOException e) {
}
});
}
}
public static void getRequest(String url, okhttp3.Callback callback) {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(request).enqueue(callback);
}
我看到 Postman 使用简单的 GET 打开图片,没有暗示任何问题。
对于这种情况,您有什么建议可以解决?
【问题讨论】:
-
我在我的一个项目中尝试了你的网址,
Glide加载它没有任何问题 -
@IonutJ.Bejan,你能分享一下小代码sn-p吗?我也尝试过使用 Glide,但没有成功。
标签: android picasso okhttp3 android-glide