【问题标题】:Android tutorial - setImageResource and Grid ViewAndroid 教程 - setImageResource 和 Grid View
【发布时间】:2014-01-14 17:15:26
【问题描述】:

我按照本教程实现了一个网格视图:

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 中显示它们

我该怎么做?请指出正确的方向并尽可能提供任何相关教程

【问题讨论】:

  • 编写自己的逻辑以通过网络获取数据或使用库通用图像加载器或 volley
  • 我更喜欢自己编写 fetcher。你知道这方面有什么好的教程吗?

标签: android gridview android-gridview android-gridlayout


【解决方案1】:
  • 从网上下载图片(我会提供URI)

本教程应该可以帮助你https://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android

  • 缓存图片

如果您想在一个应用程序生命周期中缓存或创建 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/

如果您还有其他与此主题相关的问题或不清楚的地方,请提出,我会尽力帮助您

【讨论】:

    【解决方案2】:

    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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多