0

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?

brian
  • 6,802
  • 29
  • 83
  • 124

2 Answers2

1

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) {

    }
user370305
  • 108,599
  • 23
  • 164
  • 151
1

From the Shown Code

Try this instead your last 2 lines

Bitmap photo = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(myfile.getPath()),60,60,true);
imageview.setImageBitmap(photo);

And if you have made any objects for Bitmap/String/Stream in your Download() function free them calling System.gc();

And I hope this will work.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Where should System.gc(); place? – brian Dec 19 '11 at 06:13
  • in your download code..and where you have created and used objects that are keeping image data..and using your memory...after using those Objects..call gc() – MKJParekh Dec 19 '11 at 06:21
  • 2
    let me tell you..calling System.gc() doesn't mean your memory will be freed instantlly...It will just raise a flag to OS that here are unused memory...and Memory will be freed while the Garbage Collctor runs..that done Automatically so you have no control over it..but you can just flag it. – MKJParekh Dec 19 '11 at 06:23