• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ImageView Problem

Ramesh

Lurker
Mar 14, 2009
1
0
Hi,

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-
beginners/browse_thread/thread/06da00e2b221822f
). 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.
 
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:
package com.trackaroo.apps.mobile.android.Trackmaster.bitmap;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class BitmapUtil {

    public static Bitmap getBitmap(Context context,String photoUriPath) throws Exception {
        Uri photoUri = Uri.parse(photoUriPath);
        InputStream photoStream = context.getContentResolver().openInputStream(photoUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize=2;
        Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream,null,options);
        int h = photoBitmap.getHeight();
        int w = photoBitmap.getWidth();
        if((w>h)&&(w>128)){
            double ratio = 128d/w;
            w=128;
            h=(int)(ratio*h);
        }
        else if((h>w)&&(h>128)){
            double ratio = 128d/h;
            h=128;
            w=(int)(ratio*w);
        }
         Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
         photoBitmap.recycle();
         return scaled;
    }
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones