android获取网络上的图片

// 如果缓存里面有就从缓存获取,否则网络获取图片,返回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 ;   
    }  


AsyncTask获取url图片

 
mImageView = (ImageView)this.findViewById(R.id.imageThreadConcept) ;   
new DownloadImageTask().execute(IMAGE_URL) ;   
private class DownloadImageTask extends AsyncTask<String, Void, Drawable>    
{   
           
        protected Drawable doInBackground(String... urls) {   
            return loadImageFromNetwork(urls[0]);   
        }   
  
        protected void onPostExecute(Drawable result) {   
            mImageView.setImageDrawable(result);   
        }   
}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值