android擷取網路上的圖片

來源:互聯網
上載者:User
// 如果緩衝裡面有就從緩衝擷取,否則網路擷取圖片,返回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);           }   }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.