2014-01-14 30 views
0

我實現了一個網格視圖按照本教程:Android的教程 - setImageResource和網格視圖

http://developer.android.com/guide/topics/ui/layout/gridview.html

適配器具有以下圖片引用:

// references to our images 
private Integer[] mThumbIds = { 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1, 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1, 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7 
}; 

和圖案,然後使用顯示setImageResource

imageView.setImageResource(mThumbIds[position]); 
從互聯網

  1. 下載圖片(我會提供的URI)
  2. 緩存圖像
  3. 顯示他們在GridView

我想提高這一點,...

我該怎麼做?請點我到正確的方向,並提供任何相關教程如果可能的話

+0

寫您自己的邏輯來取過網或利用圖書館數據通用圖像加載程序或凌空 –

+0

我寧願自己編寫fetcher。你知道這個有什麼好的教程嗎? – chuckfinley

回答

1
  • 下載圖片來自互聯網(我將提供URI)

本教程會幫助你https://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android

  • 緩存

你可以在你的類,如果你想擁有緩存爲一個應用程序生命週期只使用HashMap的或創建的SQLite數據庫中的圖像具有高速緩存可以存儲數據永遠https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

  • 顯示他們在GridView

您需要擴展BaseAdapter類。本教程應該可以幫助您:http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

如果你有相關的這個話題什麼的另一個問題是,不明確的只是問,我會盡力幫助你

0

的AsyncTask上加載一個ImageView的照片:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    private ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     final File cacheDir = getCacheDir(); 
     Bitmap bitmap = null; 
     if (Utill.isMemoryAvaliable(dir.getPath())){ 
      String url = urls[0]; 
      String filename = url.substring(url.lastIndexOf("/")+1,url.contains("?")?url.indexOf("?"):url.length()); 
      File f = new File(cacheDir, filename); 
      //from SD cache 
      if(!f.exists()){ 
       try { 
        Utill.DownloadFromUrl(url, filename, cacheDir); 
       } catch (IOException ex) { 
        Log.e("Error", "Download", ex); 
       } 
      } 
      if(f.exists()) 
       bitmap = decodeFile(new File(cacheDir, filename)); 
     } 
     return bitmap; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 

    private Bitmap decodeFile(File f) { 
     try { 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
      return BitmapFactory.decodeStream(new FileInputStream(f)); 
     } catch (FileNotFoundException e) { 
      Log.e("Error", "Decode File", e); 
     } 
     return null; 
    } 

} 

要下載圖片:

public static boolean downloadFromUrl(String downloadUrl, String fileName, File dir) throws IOException { 
     if (URLUtil.isValidUrl(downloadUrl)) { 
      System.setProperty("http.keepAlive", "false"); 
      URL url = new URL(downloadUrl); 
      HttpURLConnection ucon = (HttpURLConnection) url.openConnection(); 
      ucon.setRequestProperty("Connection", "Keep-Alive"); 
      ucon.setConnectTimeout(50000); 
      ucon.connect(); 
      if (ucon.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       InputStream is = url.openStream(); 
       if (is.available() > 0) { 
        BufferedInputStream bis = new BufferedInputStream(is); 
        ByteArrayBuffer baf = new ByteArrayBuffer(5000); 
        int current = 0; 
        while ((current = bis.read()) != -1) { 
         baf.append((byte) current); 
        } 
        File file = new File(dir, fileName); 
        FileOutputStream fos = new FileOutputStream(file); 
        fos.write(baf.toByteArray()); 
        fos.flush(); 
        fos.close(); 
       } 
       is.close(); 
       return true; 
      } else { 
       return false; 
      } 
     } 
     return false; 
    }