brian brian - 3 months ago 9
Android Question

Download and show the Thumbnail

I try to download a picture from URL to SD card/Download.
And I try to show its thumbnail in imageview.
Now I had below code:

try {
Download(URL); //download picture to SD card/Download
File myfile = new File(Environment.getExternalStorageDirectory() + "/Download/", filename);
Drawable photo = null;
photo = Drawable.createFromPath(myfile.getPath());
imageview.setBackgroundDrawable(photo);
}


It show the original picture.
But when the picture is large.
The memory error occurs.
So I want to show the smaller picture.
How should I do to generate the thumbnail and show it?
Or how to use the thumbnail generate by Android system?

Answer

Use Bitmap, Something like,

    try     
    {
        Download(URL);  //download picture to SD card/Download

        final int THUMBNAIL_SIZE = 64;

        FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/Download/", filename);
        Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
        imageview.setImageBitmap(imageBitmap);

    }
    catch(Exception ex) {

    }