java - 无法从 URL Java 获取特定图像
问题描述
我正在使用来自此 URL https://orteil.dashnet.org/cookieclicker/img的图像的 Android Studio 中的应用程序
此目录中的所有图片都可以使用,除了这张图片:https ://orteil.dashnet.org/cookieclicker/img/buildings.png
这张图片的奇怪之处在于,如果我将它保存在可绘制文件夹中,它会很好地加载这张图片。当我运行它时,InputStream 行抛出“java.net.ProtocolException: Too many follow-up requests: 21”。这是我无法从目录加载的唯一图像。有人可以解释为什么 InputStream 会为此图像抛出 ProtocolException 吗?
这是我正在运行的代码。注意-此代码在新线程上运行
//src is the bad url as a string
//myBitmap is a Bitmap array
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream input = connection.getInputStream();
myBitmap[0] = BitmapFactory.decodeStream(input);
} catch ( IOException e) {
e.printStackTrace();
}
解决方案
为什么你使用流连接来加载图像......你可以在Glide中获得更好的性能并处理此类连接问题
如果您不想使用 URL 中的位图,请使用:
GlideApp.with(itemView.getContext())
.asBitmap()
.load(imageUrl) //https://orteil.dashnet.org/cookieclicker/img/buildings.png
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
});
}
推荐阅读
- azure-cosmosdb - Cosmos Db 中容器级别的事务
- powershell - Foreach,最后一个条件不需要为它找到的每个虚拟机“确定”
- mysql - Sakila 模式显示所有表的 NULL 值?
- kotlin-coroutines - Kotlin 协程的长任务
- javascript - 对 API 调用感到困惑。计算当前日期和未来日期的时间
- android - 如何获得电子邮件意图的结果?结果代码不正确
- sequelize.js - 如果一行失败,则继续批量创建不会失败
- c# - Timer Elapsed Event 未命中目标方法
- python - 如何使用长度对集合字典进行排序?
- python - 从目录中选择随机文件时出错