来自资源的大图像获得异常

时间:2012-11-25 17:39:46

标签: java android bitmap android-viewpager

我按照此链接中的示例进行操作: Viewpager Example in android

它的工作正常但是当我放置我自己的高分辨率图像(大尺寸)时,它给了我例外:

java.lang.OutOfMemoryError:位图大小超过VM预算

我发布了一个关于该问题的旧问题,但由于这个原因,它关闭了它的重复,我尝试并搜索了很多,最后我发现解决方案是:通过以下方式缩放我的图像以避免内存异常:

作者在stackfllow和android开发网站的建议和答案,我以波纹管代码结束,这也以同样的例外结束,我认为我的代码中有一些错误因为我还在学习java和android开发,但那是什么我可以结束,欢迎任何帮助或建议,

谢谢。

我的代码:

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堆栈

   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)

2 个答案:

答案 0 :(得分:1)

请尝试以下提示。

- Android适用于.png张图片,其中.jpg格式的相同尺寸图片会创建OutOfMemoryError。< / p>

请参阅此链接以获取官方Android开发者网站的解决方案:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

答案 1 :(得分:0)

你永远不会使用这种方法来缩放图像,你的inSampleSize也需要是2的幂(我相信)。这些图像太大了(你在上一篇文章中说过的是4.5 MB)。在存储的巨大位图之上,视图寻呼机在任何给定时间在内存中保留3个视图。为视图寻呼机分配的内存类似于> 13.5mb。使用以下内容替换实例化视图:

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;

如果仍然提供错误,请使用options.inSampleSize = 8;