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 8891019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:00+00:00 2026-06-14T22:38:00+00:00

I follow the example in this link : Viewpager Example in android its work

  • 0

I follow the example in this link :
Viewpager Example in android

its work fine but when i placed my own high resolution images (large in size ), it gave me exception :

java.lang.OutOfMemoryError: bitmap size exceeds VM budget.

i post an old question about that but it closed coz its duplicated for that reason , i tried and searched alot, finally i found the solution which is : scaling my images to avoid memory exception by following :

author advice and answers here in stackfllow and android development site , i ended with the bellow code , which also end with same exception, i think there is some wrong in my code coz im still learning java and android development , but that what i can end with , please any help or advice will be appreciated ,

thanks .

my code:

ViewPagerAdapter

  public class ViewPagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];

public ViewPagerAdapter(Activity act, int[] imgArra) {
    imageArray = imgArra;
    activity = act;
                           }

public int getCount() {
    return imageArray.length;
                        }

public Object instantiateItem(View collection, int position) {
    ImageView view = new ImageView(activity);
    view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    view.setScaleType(ScaleType.FIT_XY);
    view.setBackgroundResource(imageArray[position]);
    ((ViewPager) collection).addView(view, 0);
    return view;
                              }

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
                              }

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
                                     }

@Override
public Parcelable saveState() {
    return null;
                                       }
public static Bitmap decodeSampledBitmapFromResource(String imageArra,
        int reqWidth, int reqHeight) {      

        // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imageArra, options);

 // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(imageArra, options);
                                       }


    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
                      }
                          }
    return inSampleSize;}}

PageIndicatorActivity:

    public class PageIndicatorActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
                                 }

private int imageArra[] = { R.drawable.one, R.drawable.two,
        R.drawable.three, R.drawable.four,
        R.drawable.five, R.drawable.six,
        R.drawable.seven, R.drawable.eight,R.drawable.nine,
                          R.drawable.ten };  }

logcat stack :

   FATAL EXCEPTION: main
     java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1709)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.view.View.setBackgroundResource(View.java:7586)
at com.horizontalscrollviewwithpageindicator.ViewPagerAdapter.instantiateItem
        (ViewPagerAdapter.java:33)
at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
at android.support.v4.view.ViewPager.populate(ViewPager.java:875)
at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
at android.view.View.measure(View.java:8366)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8366)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8366)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8366)
at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
  • 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-14T22:38:01+00:00Added an answer on June 14, 2026 at 10:38 pm

    You never use the method to scale the images, also your inSampleSize needs to be a power of two(i believe). These images are way too big (4.5 mb that you stated in your previous post). On top of a huge bitmap being stored, the view pager keeps 3 views in memory at any given time. Your memory allocated for your view pager is something like >13.5mb. Replace your instantiate view with the following:

    ImageView myView = new ImageView(context);
    BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
    
    Bitmap bitmap = BitmapFactory.decodeResource(activity.getResouces(), imageArray[position], options );
    myView.setImageBitmap(bitmap);
    
    ((ViewPager) view).addView(myView);   
    
    return myView;
    

    If this still provides an error, use options.inSampleSize = 8;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow the examples in this link and this one but
I am new to maven and I am trying to follow this example but
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
I'm a newbie to sql and am trying to follow this example here: http://net.tutsplus.com/tutorials/php/a-better-login-system/
This is a follow-up question to this one . Consider this example: #include <iostream>
I all, I just follow this great example: http://qt-project.org/doc/qt-4.8/tools-customcompleter.html I copy the code exactly
I'm a beginner with Json, I follow this tutorial the example I have written
Ok, in my code I have for example, this: $('.follow').click(function(){ var myself = $(this);
What i have this the follow code foreach (HtmlNode link in htmldocObject.DocumentNode.SelectNodes(//a[@href])) { HtmlAttribute

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.