参照http://keeponmoving.iteye.com/blog/1515587写了一个demo,发现图片无法显示。
/*
* 从网络上获取图片,如果图片在本地存在的话就直接拿,如果不存在再去服务器上下载图片
* 这里的path是图片的地址
*/
public Uri getImageURI(String path, File cache) throws Exception {
String name = MD5.getMD5(path) + path.substring(path.lastIndexOf("."));
File file = new File(cache, name);
// 如果图片存在本地缓存目录,则不去服务器下载
if (file.exists()) {
return Uri.fromFile(file);//Uri.fromFile(path)这个方法能得到文件的URI
} else {
// 从网络上获取图片
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}
return null;
}
} 代码读到红色字体部分的时候,跳到下面代码的catch部分。求解。
// 后台运行的子线程子线程
@Override
protected Uri doInBackground(String... params) {
try {
return service.getImageURI(params[0], cache);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} 网络httpurlconnection图片
/*
* 从网络上获取图片,如果图片在本地存在的话就直接拿,如果不存在再去服务器上下载图片
* 这里的path是图片的地址
*/
public Uri getImageURI(String path, File cache) throws Exception {
String name = MD5.getMD5(path) + path.substring(path.lastIndexOf("."));
File file = new File(cache, name);
// 如果图片存在本地缓存目录,则不去服务器下载
if (file.exists()) {
return Uri.fromFile(file);//Uri.fromFile(path)这个方法能得到文件的URI
} else {
// 从网络上获取图片
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}
return null;
}
} 代码读到红色字体部分的时候,跳到下面代码的catch部分。求解。
// 后台运行的子线程子线程
@Override
protected Uri doInBackground(String... params) {
try {
return service.getImageURI(params[0], cache);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} 网络httpurlconnection图片
要说异常的话,可能是这句:
Type safety:Unchecked cast from Object to List<ProductInfo> (List<ProductInfo>) msg.obj