android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
2014-05-11 23:36
411 查看
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;
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 Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
- [Android]java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- Android下载文件提示文件不存在。。。 java.io.FileNotFoundException
- Android在文件操作时出现java.io.FileNotFoundException
- Android异常--java.io.FileNotFoundException下载文件时异常
- Android异常java.io.FileNotFoundException:(Is a directory)
- Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap open failed: ENOEN
- web 项目log4j配置无法获取文件(java.io.FileNotFoundException: /logs/log.txt (No such file or directory))猜想
- Android java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- keytool 错误: java.io.FileNotFoundException: android.keystore (拒绝访问。)
- Android java.io.FileNotFoundException on Android, open failed: ENOENT (No such file or directory)
- keytool 错误: java.io.FileNotFoundException: MyAndroidKey.keystore (拒绝访问).
- keytool错误: java.io.FileNotFoundException: android.key (Permission denied)
- Android java.io.FileNotFoundException:*** (Permission denied)
- Android java.io.FileNotFoundException: open failed: EACCES (Permission denied) 问题未解决!!
- Android异常java.io.FileNotFoundException:(Is a directory)
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- Android 混淆提示 java.io.FileNotFoundException: ...\proguard
- 在tomcat与weblogic下获取项目路径的区别(如报错:java.io.FileNotFoundException)
- java.io.FileNotFoundException: URL [jndi:/localhost/myBlogSys/WEB-INF/classes/] cannot be resolved t