1. Check out our companion app, Forums for Android! Download from Google Play

ImageView Problem

Discussion in 'Application Development' started by Ramesh, Mar 30, 2009.

  1. Ramesh

    Ramesh New Member
    Thread Starter

    Mar 14, 2009

    I am working on an android project where the local and remote image
    files and their thumbnails are to be displayed. The imageview works
    perfectly till the thumbnail part. After that it crashes depending
    upon the size of the image file. For example, if the size was ~380kb,
    it will open once and then crash the next time you click on its
    thumbnail. An image of size around ~150 kb opens properly 3 times and
    then crashes the 4th time.

    It seems like a out of memory error but I have not been able to work
    out a solution. I tried to force close the activity before launching a
    new one but that didn't help either. Does anyone has any idea as to
    how to work around this issue? I have seen similar problem being
    posted on the google group (http://groups.google.com/group/android-
    ). Its not the same but
    seems like its also the result of an out of memory state.

    Any help or pointers will be greatly appreciated.

    Thanks in advance.


  2. fryguy

    fryguy Member

    Mar 30, 2009
    At the Race Track
    Are you scaling a bitmap to get your thumbnails? Are you recycling your bitmap? I have Bitmap utility that does scaling for me. This might help:

    Code (Text):
    1. package com.trackaroo.apps.mobile.android.Trackmaster.bitmap;
    3. import java.io.InputStream;
    5. import android.content.Context;
    6. import android.graphics.Bitmap;
    7. import android.graphics.BitmapFactory;
    8. import android.net.Uri;
    10. public class BitmapUtil {
    12.     public static Bitmap getBitmap(Context context,String photoUriPath) throws Exception {
    13.         Uri photoUri = Uri.parse(photoUriPath);
    14.         InputStream photoStream = context.getContentResolver().openInputStream(photoUri);
    15.         BitmapFactory.Options options = new BitmapFactory.Options();
    16.         options.inSampleSize=2;
    17.         Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream,null,options);
    18.         int h = photoBitmap.getHeight();
    19.         int w = photoBitmap.getWidth();
    20.         if((w>h)&&(w>128)){
    21.             double ratio = 128d/w;
    22.             w=128;
    23.             h=(int)(ratio*h);
    24.         }
    25.         else if((h>w)&&(h>128)){
    26.             double ratio = 128d/h;
    27.             h=128;
    28.             w=(int)(ratio*w);
    29.         }
    30.          Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
    31.          photoBitmap.recycle();
    32.          return scaled;
    33.     }
    34. }

Share This Page