-1

I am using universal image loader for loading images in grid view and loading preview of images, Out of memory Error issue is keeps on occurs. I have tried lot of methods changing configuration, display options. but no use. Please give right way to solve it.

Here is my code for displaying image using universal image loader lib,

ImageLoader imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration imageLoaderconfig = new ImageLoaderConfiguration.Builder(
                ImagePreview.this).threadPoolSize(1)
                .writeDebugLogs().memoryCache(new WeakMemoryCache())
                .build();
        imageLoader.init(imageLoaderconfig);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.empty_icon)
                .bitmapConfig(Config.RGB_565)
                .showImageForEmptyUri(R.drawable.image_for_empty_url)

                .imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();

        imageLoader.displayImage("file://"
                + CommonVariables.preview_image_path, image_preview,
                options, new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                            FailReason failReason) {
                        // TODO Auto-generated method stub
                        findViewById(R.id.pb_pd).setVisibility(
                                View.INVISIBLE); // progress bar
                        Toast.makeText(getApplicationContext(),
                                "Oops ! Please try again later",
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        // TODO Auto-generated method stub
                        Animation anim = AnimationUtils.loadAnimation(
                                ImagePreview.this, R.anim.fade_in);
                        findViewById(R.id.pb_pd).setVisibility(
                                View.INVISIBLE); // progress bar
                        image_preview.setAnimation(anim);
                        anim.start();
                    }

                    @Override
                    public void onLoadingCancelled(String imageUri,
                            View view) {
                        // TODO Auto-generated method stub
                        findViewById(R.id.pd).setVisibility(View.INVISIBLE); // progress bar
                    }
                });

Exception: (Log Cat)

10-22 12:30:00.414: E/AndroidRuntime(6908): FATAL EXCEPTION: main
10-22 12:30:00.414: E/AndroidRuntime(6908): java.lang.OutOfMemoryError
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:546)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:320)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.decodeSampledBitmapFromUri(Home.java:669)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.addDateTime(Home.java:413)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.callmethods(Home.java:356)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.onCreate(Home.java:236)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.app.Activity.performCreate(Activity.java:5020)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
NightFury
  • 13,436
  • 6
  • 71
  • 120

2 Answers2

0

Try creating thumbnail of images. This worked for me

final int THUMBSIZE = 64;
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), 
                    THUMBSIZE, THUMBSIZE);
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
0

This error occurs when the images are loaded on virtual memory when it is less. This can be solved by creating a bitmap scaled images. Take all drawables in an array and just create the rescaled bitmap of it like this.

Bitmap bit = BitmapFactory.decodeResource(getApplicationContext()
            .getResources(), drawable);
    if (bit != null) {
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bit, width,
                height, true);
        imageView.setBitmap(resizedBitmap);
        }

The rescaled bitmap will not take more virtual memory and loads the images easily

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int height=bd.getBitmap().getHeight();
int width=bd.getBitmap().getWidth();
Sridhar
  • 95
  • 1
  • 8