Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8254173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:06:18+00:00 2026-06-08T01:06:18+00:00

I’m getting an error saying that the Bitmaps are using to much memory. I

  • 0

I’m getting an error saying that the Bitmaps are using to much memory.

I know that I should use bitmap.recyle() but I don’t know where to put it, wherever I put it I get an error saying that I’m trying to use a recycled bitmap.

If anyone can help that would be great.

Here is my relevant code:

public class PictureViewer extends SherlockActivity implements
    android.view.GestureDetector.OnGestureListener {

private ViewFlipper viewFlipper = null;
private GestureDetector gestureDetector = null;
ArrayList<Integer> number = new ArrayList<Integer>();
DownloadBitmap bit = new DownloadBitmap();

int j = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.pictureviewer);
    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
    gestureDetector = new GestureDetector(this);

    for (int i = 1; i <= 65; ++i)
        number.add(i);
    Collections.shuffle(number);

    loadImage();
    loadImage();
}

public void loadImage() {

    if (j == 65) { // Change this number to exact ammount of pictures
        j = 1;
    }
    int next = number.get(j);
    j++;

    ImageView image = new ImageView(this);
    Bitmap bitmap = bit.createBitmapFromUrl("http://comedyzone.mobi/img" + next + ".jpg");
    WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(bitmap);
    image.setImageBitmap(mBitmapReference.get());
    image.setScaleType(ImageView.ScaleType.FIT_XY);
    viewFlipper.addView(image, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    if (arg0.getX() - arg1.getX() > 120) {

        this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_in));
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_left_out));
        this.viewFlipper.showNext();
        loadImage();
        return true;
    } else if (arg0.getX() - arg1.getX() < -120) {
        this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_in));
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                R.anim.push_right_out));
        this.viewFlipper.showPrevious();
        loadImage();
        return true;
    }
    return true;
}

@Override
public void onLongPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return this.gestureDetector.onTouchEvent(event);
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:06:19+00:00Added an answer on June 8, 2026 at 1:06 am

    I know that I should use bitmap.recyle()

    Calling recycle is not necessary.

    Last year at Google IO, there was a talk given precisely on this topic.

    Google I/O 2011: Memory management for Android Apps – You should definitely watch all of this, it’s worth the time.

    Making a WeakReference to your Bitmap object is a good start towards better Bitmap management. For example:

    Bitmap bitmap = DownloadImage("http://comedyzone.mobi/img" + next + ".jpg");
    WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(bitmap);
    image.setImageBitmap(mBitmapReference.get());
    

    Displaying Bitmaps Efficiently

    These are Android training classes you should read through too.

    Also, this is a class I wrote to download an image from a URL. You should consider using it in place of your DownloadImage method, it’s much more efficient.

    DownloadBitmap

    public class DownloadBitmap {
    
    private static String LOG_TAG = DownloadBitmap.class.getName();
    
    /**
     * @param url
     * @return Bitmap image from the interwebs
     */
    static Bitmap createBitmapFromUrl(String url) {
        final Bitmap mBitmap = readBitmapFromNetwork(url);
        final WeakReference<Bitmap> mBitmapReference = new WeakReference<Bitmap>(mBitmap);
        if (mBitmapReference.get() != null)
            return mBitmapReference.get();
        return null;
    }
    
    /**
     * @param urlString The URL to read the bitmap from.
     * @return A Bitmap image or null if an error occurs.
     */
    private static Bitmap readBitmapFromNetwork(String urlString) {
        InputStream mInputStream = null;
        FlushedInputStream mFlushedInputStream = null;
        Bitmap mBitmap = null;
        WeakReference<Bitmap> mBitmapReference = null;
        try {
            final BitmapFactory.Options mOptions = new BitmapFactory.Options();
            mOptions.inPurgeable = true;
            mOptions.inDither = false;
            final URL mUrl = new URL(urlString);
            final URLConnection mConnection = mUrl.openConnection();
            mConnection.connect();
            mInputStream = mConnection.getInputStream();
            mFlushedInputStream = new FlushedInputStream(mInputStream);
            mBitmap = BitmapFactory.decodeStream(mFlushedInputStream, null, mOptions);
            mBitmapReference = new WeakReference<Bitmap>(mBitmap);
        } catch (MalformedURLException e) {
            if (BuildConfig.DEBUG)
                Log.e(LOG_TAG, "Bad image URL", e);
            return null;
        } catch (IOException e) {
            if (BuildConfig.DEBUG)
                Log.e(LOG_TAG, "Could not get remote image", e);
            return null;
        } finally {
            try {
                if (mInputStream != null)
                    mInputStream.close();
                if (mFlushedInputStream != null)
                    mFlushedInputStream.close();
            } catch (IOException e) {
                if (BuildConfig.DEBUG)
                    Log.e(LOG_TAG, "Error closing stream.");
                return null;
            }
        }
        if (mBitmapReference.get() != null)
            return mBitmapReference.get();
        return null;
    }
    
    /**
     * An InputStream that skips the exact number of bytes provided, unless it
     * reaches EOF.
     */
    static class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }
    
        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int bytes = read();
                    if (bytes < 0) {
                        break;
                    } else {
                        bytesSkipped = 1;
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.