Question

i implemented a Grid View by following this tutorial:

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

The adapter has references to the following pictures:

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

And the pictures are then displayed using setImageResource:

imageView.setImageResource(mThumbIds[position]);

I would like to improve this and...:

  1. Download pictures from the internet (i will provide URIs)
  2. Cache the images
  3. Display them in the GridView

How do I do this? Please point me to the right direction and provide any relevant tutorials if possible

Was it helpful?

Solution

  • Download pictures from the internet (i will provide URIs)

This tutorial should help you https://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android

  • Cache the images

You can use just HashMap in your class if you want have cache for one application lifecycle or create SQLite database to have cache you can store data forever https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

  • Display them in the GridView

You need extend BaseAdapter class. This tutorial should help you: http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

If you will have other question relevant to this topic or something is not clear just ask, I'll try help you

OTHER TIPS

AsyncTask to load the pictures on a 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;
    }

}

To download the pictures:

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top