1

I have an App, where I load images from a server. Because of this my app leads to Outofmemory Error. I have caught the exception so that my app is now prevented from being force closed. But my app stops loading images in the place where the exception has occurred. So is there a way I could restart my activity after the exception has been caught, so that my memory is freed up and activity loads images from the first once again.

Much needed. Any help is appreciated.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240

5 Answers5

0

You should not be catching Error's in general and OutOfMemoryError in particular. After OOM you can never be sure what is the state of your application.

Instead, you should look into improving your algorithm so OOM does not occur. Describe specific use case (may be in separate question) and somebody can offer you an advice.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • @Dr.Dredel From http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html - section 11.5 `The class Error and its subclasses are exceptions from which ordinary programs are not ordinarily expected to recover`. So yes - in most cases you should not be catching errors even though it's technically possible. OOM Error is one of the worst as it leaves system in unpredictable state. – Alex Gitelman Jun 01 '11 at 04:54
0

Restarting the current activity all of a sudden is not going to be a great User experience. If possible try clearing Images from memory that are not not shown to the user(so it releases memory that it has occupied).

If still you want to restart the current activity, use the following:

void restartActivity()
{
    CurrentActivity.this.finish();
    Intent mIntent = new Intent(CurrentActivity.this, CurrentActivity.class);
    startActivity(CurrentActivity);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
AjOnFire
  • 2,868
  • 3
  • 25
  • 39
  • `clearing Images from memory` How do i achieve this. this is what I am actually looking for. I have tried system.gc(), bitmap.recycle(). But my problem is not solved. Can you suggest an idea. – Andro Selva Jun 01 '11 at 06:52
  • Can you be more specific about how you have implemented the images in your App? If you are using listView, clear all images (by using bitmap = null) from the list except those that are currently shown to the user (i.e) from the first visible position to last visible position when memory is running low and then call System.gc()... – AjOnFire Jun 01 '11 at 17:15
  • Can you provide additional info on how you are using the above method? – AjOnFire Nov 27 '13 at 17:26
0

You use an Unknown exception handler to get notified when you app crashed , from there on you can load your activity again. But its very important you make sure that this occurs only as many times you see a unicorn. It is best you fix the problem rather than work around it.

Here is a link which will help with UEH : How do I obtain crash-data from my Android application?

Community
  • 1
  • 1
Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47
0

I think the issue is in loading images from server which increased the memory causing the application to be in OutOfMemmory Error. You have to avoid your application to take such a huge memory. YOu can use LazyLoading Concept to load images.

Have a look for sample program here

or have a look here also

public class DrawableManager {
    private final Map<String, Drawable> drawableMap;

    public DrawableManager() {
        drawableMap = new HashMap<String, Drawable>();
    }

    public Drawable fetchDrawable(String urlString) {
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        }

        Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");
            drawableMap.put(urlString, drawable);
            Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
                    + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                    + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
            return drawable;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        } catch (IOException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
        if (drawableMap.containsKey(urlString)) {
            imageView.setImageDrawable(drawableMap.get(urlString));
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageView.setImageDrawable((Drawable) message.obj);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                //TODO : set imageView to a "pending" image
                Drawable drawable = fetchDrawable(urlString);
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            }
        };
        thread.start();
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }

}

Thanks Deepak

Community
  • 1
  • 1
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0
Intent  intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);

this will refresh or restart the activity.

Pang
  • 9,564
  • 146
  • 81
  • 122
varun bhardwaj
  • 1,522
  • 13
  • 24