简体   繁体   中英

java.lang.OutOfMemoryError: bitmap size exceeds VM budget in Swipey-Viewpager

I'm using Swipey-ViewPagerIndicator. I have three fragments. Layout of these fragment contains ImageView. Here is fragment_swipeytab.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright 2011 Peter Kuterna

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff" 
    android:id="@+id/RootView">

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        />

</FrameLayout>

Image is set programmatically for three fragments. Here is SwipeyTabFragment.java:

 /*
     * Copyright 2011 Peter Kuterna
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    package com.gmail.suankap.firstapp;

    import java.io.IOException;

    import net.peterkuterna.android.apps.swipeytabs.R;
    import android.content.Intent;
    import android.content.res.AssetFileDescriptor;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.MediaController;
    import android.widget.Toast;
    import android.widget.MediaController.MediaPlayerControl;
    import android.widget.TextView;

    public class SwipeyTabFragment extends Fragment {

        public static Fragment newInstance(String title) {
            SwipeyTabFragment f = new SwipeyTabFragment();
            Bundle args = new Bundle();
            args.putString("title", title);
            f.setArguments(args);
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            ViewGroup root = (ViewGroup) inflater.inflate(
                    R.layout.fragment_swipeytab, null);
            final String title = getArguments().getString("title");
            ((ImageView) root.findViewById(R.id.imageView1))
                    .setImageResource(R.drawable.both);
            Log.v("TAGS", title);
            return root;
        }

    }

Here is Logcat:

 09-28 08:41:25.546: E/AndroidRuntime(376): FATAL EXCEPTION: main
    09-28 08:41:25.546: E/AndroidRuntime(376): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.content.res.Resources.getDrawable(Resources.java:581)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.ImageView.resolveUri(ImageView.java:501)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.ImageView.setImageResource(ImageView.java:280)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.gmail.suankap.firstapp.SwipeyTabFragment.onCreateView(SwipeyTabFragment.java:59)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:696)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:668)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.View.draw(View.java:6883)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.View.draw(View.java:6883)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.os.Looper.loop(Looper.java:123)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at java.lang.reflect.Method.invoke(Method.java:507)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at dalvik.system.NativeStart.main(Native Method)
    09-28 08:41:28.314: I/Process(376): Sending signal. PID: 376 SIG: 9

The problem is ANR - OutOfMemory Error when I swipe the fragments. Why does OutOfMemory occur when there there only one image in only three fragments( also the same resource R.drawable.both )? How to solve it?

Your one image takes up 1944x1944*4 bytes == 14.5 MB. Now your three images only take up to 43.5 MB. And an app is allocated from 32 MB to 64 MB depending on the device. If it's the same image, you can also make it the background of your Activity and not fragments.

您的图片R.drawable.both的大小是多少?

I had the same problem with displaying an image. Try using a really small image and see if it works. I ended up having to resize my image to roughly 800w x 1000h for the app to not crash. This thread helped me: Android: out of memory exception in Gallery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM