获取网络图片的方法(如果手机缓存里面有就从缓存获取),我以前写的,比较原始:
- ImageView mImageView = (ImageView)this.findViewById(R.id.imageview);
- String imagePath = getImagePath(context, photoURL); // context:上下文 ,photoURL:图片的url路径
- mImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
ImageView mImageView = (ImageView)this.findViewById(R.id.imageview);
String imagePath = getImagePath(context, photoURL); // context:上下文 ,photoURL:图片的url路径
mImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
getImagePath()方法:
- // 获取网络图片,如果缓存里面有就从缓存里面获取
- public static String getImagePath(Context context, String url) {
- if(url == null )
- return "";
- String imagePath = "";
- String fileName = "";
- // 获取url中图片的文件名与后缀
- if(url!=null&&url.length()!=0){
- fileName = url.substring(url.lastIndexOf("/")+1);
- }
- // 图片在手机本地的存放路径,注意:fileName为空的情况
- imagePath = context.getCacheDir() + "/" + fileName;
- Log.i(TAG,"imagePath = " + imagePath);
- File file = new File(context.getCacheDir(),fileName);// 保存文件,
- if(!file.exists())
- {
- Log.i(TAG, "file 不存在 ");
- try {
- byte[] data = readInputStream(getRequest(url));
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
- data.length);
- bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(
- file));
- imagePath = file.getAbsolutePath();
- Log.i(TAG,"imagePath : file.getAbsolutePath() = " + imagePath);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- }
- }
- return imagePath;
- } // getImagePath( )结束。
// 获取网络图片,如果缓存里面有就从缓存里面获取
public static String getImagePath(Context context, String url) {
if(url == null )
return "";
String imagePath = "";
String fileName = "";
// 获取url中图片的文件名与后缀
if(url!=null&&url.length()!=0){
fileName = url.substring(url.lastIndexOf("/")+1);
}
// 图片在手机本地的存放路径,注意:fileName为空的情况
imagePath = context.getCacheDir() + "/" + fileName;
Log.i(TAG,"imagePath = " + imagePath);
File file = new File(context.getCacheDir(),fileName);// 保存文件,
if(!file.exists())
{
Log.i(TAG, "file 不存在 ");
try {
byte[] data = readInputStream(getRequest(url));
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(
file));
imagePath = file.getAbsolutePath();
Log.i(TAG,"imagePath : file.getAbsolutePath() = " + imagePath);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
return imagePath;
} // getImagePath( )结束。
getRequest( ) 方法:网络获取图片为输入了
- public static InputStream getRequest(String path) throws Exception{
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5000); // 5秒
- if(conn.getResponseCode() == 200){
- return conn.getInputStream();
- }
- return null;
- }
public static InputStream getRequest(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000); // 5秒
if(conn.getResponseCode() == 200){
return conn.getInputStream();
}
return null;
}
readInputStream( ) 方法:把输入流转化成二进制
- public static byte[] readInputStream(InputStream inStream) throws Exception{
- ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int len = 0;
- while( (len = inStream.read(buffer)) != -1 ){
- outSteam.write(buffer, 0, len);
- }
- outSteam.close();
- inStream.close();
- return outSteam.toByteArray();
- }
==============================================
- // 如果缓存里面有就从缓存获取,否则网络获取图片,返回Drawable对象
- public static Drawable loadImageFromNetwork(Context context, String imageUrl)
- {
- Drawable drawable = null;
- if(imageUrl == null )
- return null;
- String imagePath = "";
- String fileName = "";
- // 获取url中图片的文件名与后缀
- if(imageUrl!=null&&imageUrl.length()!=0){
- fileName = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
- }
- // 图片在手机本地的存放路径,注意:fileName为空的情况
- imagePath = context.getCacheDir() + "/" + fileName;
- Log.i("test","imagePath = " + imagePath);
- File file = new File(context.getCacheDir(),fileName);// 保存文件
- Log.i("test","file.toString()=" + file.toString());
- if(!file.exists()&&!file.isDirectory())
- {
- try {
- // 可以在这里通过文件名来判断,是否本地有此图片
- FileOutputStream fos=new FileOutputStream( file );
- InputStream is = new URL(imageUrl).openStream();
- int data = is.read();
- while(data!=-1){
- fos.write(data);
- data=is.read();;
- }
- fos.close();
- is.close();
- // drawable = Drawable.createFromStream(
- // new URL(imageUrl).openStream(), file.toString() ); // (InputStream) new URL(imageUrl).getContent();
- drawable = Drawable.createFromPath(file.toString());
- Log.i("test", "file.exists()不文件存在,网上下载:" + drawable.toString());
- } catch (IOException e) {
- Log.d("test", e.getMessage());
- }
- }else
- {
- drawable = Drawable.createFromPath(file.toString());
- Log.i("test", "file.exists()文件存在,本地获取");
- }
- if (drawable == null) {
- Log.d("test", "null drawable");
- } else {
- Log.d("test", "not null drawable");
- }
- return drawable ;
- }