vlad_o
vlad_o

Reputation: 657

Custom image viewing slows down

The problem

Hi there,

I'm developing an application where the user specifies some pictures and how long they are going to be on the screen.So sometimes he wants to create something like a small animation or viewing the images for a small amount of time.The problem is that after some time the images are not previewed when they should and we have a few ms of error.In the application that i'm developing time matters so I would like some help on what the problem might be.

The code

So let me explain how it works.I take the pictures from my web app and then I save them in a HashMap

Bitmap image = ImageOperations(url,String.valueOf(frameNum) + ".jpg");

ImageMap.put(String.valueOf(frameNum), image);

where the mathod ImageOperations is like that:

private Bitmap ImageOperations(String url, String saveFilename) {
        try {
            Display display = getWindowManager().getDefaultDisplay();
            InputStream is = (InputStream) this.fetch(url);
            Bitmap theImage = BitmapFactory.decodeStream(is);
            if (theImage.getHeight() >= 700 || theImage.getWidth() >= 700) {
                theImage = Bitmap.createScaledBitmap(theImage,
                        display.getWidth(), display.getHeight() - 140, true);
            }
            return theImage;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

So later I run a thread that updates the UI when the user specified.The method that updates it is this one.

public void setPictures(int NumOfFrame) {
if (frameArray.get(NumOfFrame - 1).frame_pic.contains("n/a") != true) {
ImagePlace.setImageBitmap(ImageMap.get(String.valueOf(NumOfFrame)));
} else {
        ImagePlace.setImageDrawable(null);
}

}

After we update the image we put the thread for sleep and when runs again it updates the thread.Is there something that creates the problem?Does it have to do with Garbage collection?

Thank you in advance

Upvotes: 0

Views: 73

Answers (2)

sandrstar
sandrstar

Reputation: 12643

Probably the issue is in increasing heap size when it loads additional images. I would suggest You to do some profiling so things will be much clearer and You'll get full picture of timings for the app.

Upvotes: 1

Ilya Gazman
Ilya Gazman

Reputation: 32226

First you are missing a null check at here:

ImageMap.get(String.valueOf(NumOfFrame))

And you do not recycle the old bitmap at here:

theImage.recycle(); // missing line
theImage = Bitmap.createScaledBitmap(theImage,
                        display.getWidth(), display.getHeight() - 140, true);

It may lead to outofmemory exceptions, with is most likely from your description of the problem.

Also I am not sure if BitmapFactory.decodeStream throw exception when he fails. You need to add a null point check there too.

Upvotes: 0

Related Questions