1

I've created an android app, which chooses a picture from Gallery and displays a preview.

@Override
public void onClick(View v) {
    if (v.getId()== R.id.button){

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);
    }

After the image is selected, the preview should be shown.

Yet, it works only for the first time. And later when I click back, it shows outOfMemoryException

2
  • it may happen that ur image is huge, y dont u try to display thumbnail of the same Commented Aug 20, 2013 at 11:44
  • please have a look at this : stackoverflow.com/a/16959054/1567588 Commented Aug 20, 2013 at 11:45

3 Answers 3

2

working with bitmaps in android costs you a lot of memory, which needs a hude attention because of memory leaks.

you can always use

System.gc()

to garbage collect and free up some memory.

or

bitmap.recycle();

cheack out these blog post that I used when I developed my image editing app.

Sign up to request clarification or add additional context in comments.

2 Comments

its not advised to call garbage collector in this situation. If you are using Android < 3.0, you must recycle the bitmap using Bitmap.recycle() as the Garbage Collector has no visibility of the pixel data stored in C array and you will eventually run out of memory on apps that make use of a lot of bitmaps. On Android > 3 you do not have to call recycle, but you must not keep any references to the bitmap beyond the scope of your activity.
no problem, also check out my answer, you may find it usefull, it was for me. good luck
1

Working with bitmaps in android often throws the OutOfMemory error. Bitmaps need to be handled properly. you might want to look at the following libraries used specifically for image loading and working with bitmaps in android:

https://github.com/nostra13/Android-Universal-Image-Loader

https://github.com/novoda/ImageLoader

You can also implement your own imageloader. You can find the code for that easily.

Comments

0

you are probably caching a lot of bitmaps, so you could use ImageLoader and do something like this:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this).memoryCache(new WeakMemoryCache())
                .discCache(new UnlimitedDiscCache(new File("/cache"))).build();

also try somehow to release bitmaps after you no longer need them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.