Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)

获取网络图片的方法(如果手机缓存里面有就从缓存获取),我以前写的,比较原始:

Java代码 复制代码 收藏代码
  1. ImageView mImageView = (ImageView)this.findViewById(R.id.imageview);
  2. String imagePath = getImagePath(context, photoURL); // context:上下文 ,photoURL:图片的url路径
  3. 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()方法:

Java代码 复制代码 收藏代码
  1. // 获取网络图片,如果缓存里面有就从缓存里面获取
  2. public static String getImagePath(Context context, String url) {
  3. if(url == null )
  4. return "";
  5. String imagePath = "";
  6. String fileName = "";
  7. // 获取url中图片的文件名与后缀
  8. if(url!=null&&url.length()!=0){
  9. fileName = url.substring(url.lastIndexOf("/")+1);
  10. }
  11. // 图片在手机本地的存放路径,注意:fileName为空的情况
  12. imagePath = context.getCacheDir() + "/" + fileName;
  13. Log.i(TAG,"imagePath = " + imagePath);
  14. File file = new File(context.getCacheDir(),fileName);// 保存文件,
  15. if(!file.exists())
  16. {
  17. Log.i(TAG, "file 不存在 ");
  18. try {
  19. byte[] data = readInputStream(getRequest(url));
  20. Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
  21. data.length);
  22. bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(
  23. file));
  24. imagePath = file.getAbsolutePath();
  25. Log.i(TAG,"imagePath : file.getAbsolutePath() = " + imagePath);
  26. } catch (Exception e) {
  27. Log.e(TAG, e.toString());
  28. }
  29. }
  30. return imagePath;
  31. } // 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( ) 方法:网络获取图片为输入了

Java代码 复制代码 收藏代码
  1. public static InputStream getRequest(String path) throws Exception{
  2. URL url = new URL(path);
  3. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  4. conn.setRequestMethod("GET");
  5. conn.setConnectTimeout(5000); // 5秒
  6. if(conn.getResponseCode() == 200){
  7. return conn.getInputStream();
  8. }
  9. return null;
  10. }
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( ) 方法:把输入流转化成二进制

Java代码 复制代码 收藏代码
  1. public static byte[] readInputStream(InputStream inStream) throws Exception{
  2. ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
  3. byte[] buffer = new byte[4096];
  4. int len = 0;
  5. while( (len = inStream.read(buffer)) != -1 ){
  6. outSteam.write(buffer, 0, len);
  7. }
  8. outSteam.close();
  9. inStream.close();
  10. return outSteam.toByteArray();
  11. }

 

==============================================

 

 

  1. // 如果缓存里面有就从缓存获取,否则网络获取图片,返回Drawable对象
  2. public static Drawable loadImageFromNetwork(Context context, String imageUrl)
  3. {
  4. Drawable drawable = null;
  5. if(imageUrl == null )
  6. return null;
  7. String imagePath = "";
  8. String fileName = "";
  9. // 获取url中图片的文件名与后缀
  10. if(imageUrl!=null&&imageUrl.length()!=0){
  11. fileName = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
  12. }
  13. // 图片在手机本地的存放路径,注意:fileName为空的情况
  14. imagePath = context.getCacheDir() + "/" + fileName;
  15. Log.i("test","imagePath = " + imagePath);
  16. File file = new File(context.getCacheDir(),fileName);// 保存文件
  17. Log.i("test","file.toString()=" + file.toString());
  18. if(!file.exists()&&!file.isDirectory())
  19. {
  20. try {
  21. // 可以在这里通过文件名来判断,是否本地有此图片
  22. FileOutputStream fos=new FileOutputStream( file );
  23. InputStream is = new URL(imageUrl).openStream();
  24. int data = is.read();
  25. while(data!=-1){
  26. fos.write(data);
  27. data=is.read();;
  28. }
  29. fos.close();
  30. is.close();
  31. // drawable = Drawable.createFromStream(
  32. // new URL(imageUrl).openStream(), file.toString() ); // (InputStream) new URL(imageUrl).getContent();
  33. drawable = Drawable.createFromPath(file.toString());
  34. Log.i("test", "file.exists()不文件存在,网上下载:" + drawable.toString());
  35. } catch (IOException e) {
  36. Log.d("test", e.getMessage());
  37. }
  38. }else
  39. {
  40. drawable = Drawable.createFromPath(file.toString());
  41. Log.i("test", "file.exists()文件存在,本地获取");
  42. }
  43. if (drawable == null) {
  44. Log.d("test", "null drawable");
  45. } else {
  46. Log.d("test", "not null drawable");
  47. }
  48. return drawable ;
  49. }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值