Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap loadImage(int bitmapId, Resources resources) {
        return loadImage(bitmapId, resources, 3);
    }

    public static Bitmap loadImage(int bitmapId, Resources resources, int tries) {
        Bitmap bitmap;

        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            bitmap = BitmapFactory.decodeResource(resources, bitmapId, options);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();

            System.gc();

            if (--tries > 0) {
                return loadImage(bitmapId, resources, tries);
            } else {
                return null;
            }
        }

        return bitmap;
    }
}