android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
来源:互联网 发布:山西省快乐十分软件 编辑:程序博客网 时间:2024/06/07 02:40
package com.loopj.android.image;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class WebImage implements SmartImage {
private static final int CONNECT_TIMEOUT = 5000;
private static final int READ_TIMEOUT = 10000;
private static WebImageCache webImageCache;
private String url;
public WebImage(String url) {
this.url = url;
}
public Bitmap getBitmap(Context context) {
// Don't leak context
if(webImageCache == null) {
webImageCache = new WebImageCache(context);
}
// Try getting bitmap from cache first
Bitmap bitmap = null;
if(url != null) {
bitmap = webImageCache.get(url);
if(bitmap == null) {
bitmap = getBitmapFromUrl(url);
if(bitmap != null){
webImageCache.put(url, bitmap);
}
}
}
return bitmap;
}
private Bitmap getBitmapFromUrl(String url) {
Bitmap bitmap = null;
try {
URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)");
bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;
}
public static void removeFromCache(String url) {
if(webImageCache != null) {
webImageCache.remove(url);
}
}
}
在getBitmapFromUrl方法中指定浏览器的类型即可
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C;
- android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
- connection.getInputStream()抛出java.io.FileNotFoundException
- java IO流抛出FileNotFoundException问题
- 开源项目smartImageView
- 上传文件 时FileNotFoundException:java.io.FileNotFoundException
- 解决URL java.io.FileNotFoundException: http://......
- java.io.FileNotFoundException: url(拒绝访问。)问题
- Android在文件操作时出现java.io.FileNotFoundException
- Android异常--java.io.FileNotFoundException下载文件时异常
- Android异常--java.io.FileNotFoundException下载文件时异常
- Android网络图片查看器——SmartImageView
- android异常:java.io.FileNotFoundException:Permission Denied
- Android 混淆提示 java.io.FileNotFoundException: ...
- Android 混淆提示 java.io.FileNotFoundException: ...\proguard
- 用开源代码smartimageview 实现网络图片查看
- Android--Android图像开源视图:SmartImageView
- 【Android之SmartImageView图片控件】
- java.io.FileNotFoundException翻译
- Hough变换背后的机制
- 2014年5月第二个周末总结--保守自己的心
- OGNL使用详解一:原理与常用语法方式
- 一句话的想法--路由器
- Counting Inversion Pairs in an Array
- android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
- atitit.常用编程语言的性能比较 c c++ java
- 急
- 特殊ASCII码查询
- jdk tomcat mysql安装
- 如何让vm虚拟机和主机同时上网
- Atitit .html5刮刮卡的gui实现总结
- 常用二极管,三极管参数
- GCC 4.8.2 编译安装小记