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

  • SEARCH
  • Home
  • 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 7840505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:48:24+00:00 2026-06-02T15:48:24+00:00

I’m actually getting this error on method Load image inside it i’m doing i

  • 0

I’m actually getting this error on method Load image inside it i’m doing
i set options size to:

bmOptions.inSampleSize = 1;

the method in which the error points line decodeStream()..

private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in, null, options);
            in.close();

        } catch (IOException e1) {

            return null;
        }
        return bitmap;

    }
E/dalvikvm-heap(8627): Out of memory on a 3001616-byte allocation.
at decodeStream()
at loadImage()
  • 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-02T15:48:26+00:00Added an answer on June 2, 2026 at 3:48 pm

    This class scales an image in a memory effecient way to a precise size, it also does some auto rotation, you might want to disable that or pass in the angle. Is based on post here: http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/

    import java.io.IOException;
    
    import java.io.InputStream;
    
    import android.content.ContentResolver;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.net.Uri;
    
    /**
     * http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/
     * 
     */
    public final class 
    
    BitmapScaler {
    
        /**
         * 
         * @param uri
         * @param context
         * @param newWidth
         *            Image will not exceed this width
         * @param newHeight
         *            Image will not exceed this height
         * @return
         * @throws IOException
         */
        public static Bitmap ScaleBitmap(Uri uri, Context context,
                int newWidth, int newHeight, int targetWidth, int targetHeight)
                throws IOException {
            final ContentResolver contentResolver = context.getContentResolver();
            int sample = 1;
            {
                InputStream is = contentResolver.openInputStream(uri);
                try {
                    sample = getRoughSize(is, newWidth, newHeight);
                } finally {
                    is.close();
                }
            }
            {
                InputStream is = contentResolver.openInputStream(uri);
                try {
                    Bitmap temp = roughScaleImage(is, sample);
                    try {
                        return scaleImage(temp, newWidth, newHeight, targetWidth,
                                targetHeight);
                    } finally {
                        temp.recycle();
                    }
                } finally {
                    is.close();
                }
            }
        }
    
        private static Bitmap scaleImage(final Bitmap source, final int maxWidth,
                final int maxHeight, final int targetWidth, final int targetHeight) {
    
            int newWidth = maxWidth;
            int newHeight = maxHeight;
    
            final int sourceHeight = source.getHeight();
            final int sourceWidth = source.getWidth();
            final int angle = sourceHeight > sourceWidth ? -90 : 0;
            final boolean rotate = angle != 0;
            final boolean nintey = (angle == 90) || (angle == -90);
            final int width = nintey ? sourceHeight : sourceWidth;
            final int height = nintey ? sourceWidth : sourceHeight;
    
            final float scaleByWidth = ((float) newWidth / width);
            int testNewHeight = (int) (height * scaleByWidth);
    
            float scale;
    
            if (testNewHeight > newHeight) { // then we must scale to match
                                                // newHeight instead of new width
                final float scaleByHeight = ((float) newHeight / height);
                newWidth = (int) (width * scaleByHeight);
                scale = scaleByHeight;
            } else {
                // accept the scale
                newHeight = testNewHeight;
                scale = scaleByWidth;
            }
    
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            if (rotate) {
                matrix.postRotate(angle);
                matrix.postTranslate(0, newHeight);
            }
            matrix.postTranslate((maxWidth - newWidth) / 2,
                    (maxHeight - newHeight) / 2);
    
            Bitmap b = Bitmap.createBitmap(targetWidth, targetHeight,
                    Bitmap.Config.ARGB_8888);
            Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
            Canvas c = new Canvas(b);
            c.drawBitmap(source, matrix, p);
            return b;
        }
    
        private static Bitmap roughScaleImage(InputStream is, int sample) {
            BitmapFactory.Options scaledOpts = new BitmapFactory.Options();
            scaledOpts.inSampleSize = sample;
            return BitmapFactory.decodeStream(is, null, scaledOpts);
        }
    
        private static int getRoughSize(InputStream is, int newWidth, int newHeight) {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, o);
    
            return getRoughSize(o.outWidth, o.outHeight, newWidth, newHeight);
        }
    
        private static int getRoughSize(int width, int height, int newWidth,
                int newHeight) {
            int sample = 1;
    
            while (true) {
                if (width / 2 < newWidth || height / 2 < newHeight) {
                    break;
                }
                width /= 2;
                height /= 2;
                sample *= 2;
            }
            return sample;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have a reasonable size flat file database of text documents mostly saved in

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.