【问题标题】:Activity cannot be loaded. outofMemory Runtime Exception无法加载活动。 outofMemory 运行时异常
【发布时间】:2014-08-21 09:47:03
【问题描述】:

我正在开发一个使用许多 jpeg 图像的应用程序。它总是给出 outOfMemory 错误异常。然后我尝试解码它。但它仍然给出了错误。请帮我解决一下这个。我不明白错误在哪里。 提前致谢。

public class Kumana extends FragmentActivity implements LocationListener{

    GoogleMap mGoogleMap;
    ArrayList<LatLng> mMarkerPoints;
    double mLatitude = 0;
    double mLongitude = 0;
    Bitmap mCurrentBitmap=null;
    BitmapFactory.Options mBitmapOptions;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private ViewFlipper mViewFlipper,mViewFlipper2,mViewFlipper3;
    private AnimationListener mAnimationListener;
    ImageView iv1, iv2, iv3,iv4,iv5,iv6,iv7,iv8,iv9,iv10,iv11,iv12;
    private Context mContext;
    int mCurrentIndex=0;
    @SuppressWarnings("deprecation")
    private final GestureDetector detector = new GestureDetector(
            new SwipeGestureDetector());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kumana);

        final int[] imgIDs={R.drawable.blackneckedstork,R.drawable.eurasianspoonbill,R.drawable.greaterrackettaileddrongo,
                R.drawable.kumana5,R.drawable.lesseradjunt,R.drawable.lesserwhistlingduck,R.drawable.malabartrogan,R.drawable.redfacedmalkoha,
                R.drawable.sirkeermalkoha,R.drawable.thegreatthickknee,R.drawable.yellowfootedgreenpigeon};

        iv1=(ImageView)findViewById(R.id.imgvw_kumana1);
        iv2=(ImageView)findViewById(R.id.imgvw_kumana2);
        iv3=(ImageView)findViewById(R.id.imgvw_kumana3);
        iv4=(ImageView)findViewById(R.id.imgvw_kumana_breeding3);
        iv5=(ImageView)findViewById(R.id.imgvw_kumana_breeding1);
        iv6=(ImageView)findViewById(R.id.imgvw_kumana_breeding2);
        iv7=(ImageView)findViewById(R.id.imgvw_kumana_breeding4);
        iv8=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants1);
        iv9=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants2);
        iv10=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants3);
        iv11=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants4);
        iv12=(ImageView)findViewById(R.id.imgvw_kumanaDesc6_RareMigrants5);

        mBitmapOptions = new BitmapFactory.Options();
        mBitmapOptions.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
        mCurrentBitmap=Bitmap.createBitmap(mBitmapOptions.outWidth,mBitmapOptions.outHeight,Bitmap.Config.ARGB_8888);
        mBitmapOptions.inJustDecodeBounds=false;
        mBitmapOptions.inBitmap=mCurrentBitmap;
        mBitmapOptions.inSampleSize=1;
        BitmapFactory.decodeResource(getResources(), R.drawable.blackneckedstork);
        iv1.setImageBitmap(mCurrentBitmap);
        mCurrentIndex=(mCurrentIndex+1)%imgIDs.length;
        BitmapFactory.Options bitmapOptions=null;
        bitmapOptions=mBitmapOptions;
        bitmapOptions.inBitmap=mCurrentBitmap;
        mCurrentBitmap=BitmapFactory.decodeResource(getResources(), imgIDs[mCurrentIndex],bitmapOptions);
        iv1.setImageBitmap(mCurrentBitmap);

        mContext = this;
        mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana1);
        mViewFlipper2 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana2);
        mViewFlipper3 = (ViewFlipper) this.findViewById(R.id.view_flipper_kumana3);

        // sets auto flipping
        mViewFlipper.setAutoStart(true);
        mViewFlipper.setFlipInterval(8000);
        mViewFlipper.startFlipping();

        mViewFlipper2.setAutoStart(true);
        mViewFlipper2.setFlipInterval(8000);
        mViewFlipper2.startFlipping();

        mViewFlipper3.setAutoStart(true);
        mViewFlipper3.setFlipInterval(8000);
        mViewFlipper3.startFlipping();

        mViewFlipper.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        mViewFlipper2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        mViewFlipper3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });

        // animation listener
        mAnimationListener = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // animation started event
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO animation stopped event
            }
        };



    class SwipeGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper.showNext();

                    mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper2.showNext();

                    mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    // controlling animation
                    mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper3.showNext();
                    return true;
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper.showPrevious();

                    mViewFlipper2.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper2.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper2.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper2.showPrevious();

                    mViewFlipper3.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper3.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    // controlling animation
                    mViewFlipper3.getInAnimation().setAnimationListener(mAnimationListener);
                    mViewFlipper3.showPrevious();
                    return true;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return false;
        }
    }

//Google Map methods

}









<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/kumana8" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/kumana5" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/lesserwhistlingduck" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaLbl" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/right_arrow" />

            <ImageView
                android:id="@+id/swipe_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/right_arrow2" />
        </RelativeLayout>


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/eurasianspoonbill" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird3" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/blackneckedstork" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird1" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/lesseradjunt" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird2" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumana_breeding4"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/thegreatthickknee" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc4_bird4" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowleft" />

            <ImageView
                android:id="@+id/swipe_right1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowright" />
        </RelativeLayout>

        <TextView
            android:id="@+id/kumanaDesc5_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:paddingTop="10dp"
            android:text="@string/kumanaDesc5_header"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/kirinddescripion3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:paddingTop="10dp"
            android:text="@string/kumanaDesc6_RareMigrants"
            android:textSize="15sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper_kumana3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/yellowfootedgreenpigeon" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants1" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/greaterrackettaileddrongo" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants2_graterRacket" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/malabartrogan" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants3" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants4"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/redfacedmalkoha" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants4" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <ImageView
                        android:id="@+id/imgvw_kumanaDesc6_RareMigrants5"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:adjustViewBounds="true"
                        android:contentDescription="@string/imageVw"
                        android:scaleType="centerCrop"
                        android:src="@drawable/sirkeermalkoha" />

                    <TextView
                        style="@style/ImageTitle"
                        android:text="@string/kumanaDesc6_RareMigrants4" />
                </RelativeLayout>
            </ViewFlipper>

            <ImageView
                android:id="@+id/swipe_left1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowleft" />

            <ImageView
                android:id="@+id/swipe_right1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:contentDescription="@string/imageVw"
                android:src="@drawable/arrowright" />
        </RelativeLayout>

        <fragment
            android:id="@+id/map_kumana"
            android:layout_width="match_parent"
            android:layout_height="466dp"
            class="com.google.android.gms.maps.SupportMapFragment" />
    </LinearLayout>

</ScrollView>

threadid=1:线程以未捕获的异常退出(组=0x41df1700) E/AndroidRuntime(25252): 致命异常: main

E/AndroidRuntime(25252): java.lang.RuntimeException: 无法启动 活动 组件信息{com.isuru.allaboutkataragama/com.isuru.allaboutkataragama.Kumana}: android.view.InflateException:二进制 XML 文件第 165 行:错误 膨胀类

E/AndroidRuntime(25252): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) E/AndroidRuntime(25252):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)

08-21 15:00:18.370: E/AndroidRuntime(25252): 由: java.lang.reflect.InvocationTargetException

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 java.lang.reflect.Constructor.constructNative(Native Method) 08-21 15:00:18.370:E/AndroidRuntime(25252):在 java.lang.reflect.Constructor.newInstance(Constructor.java:417) 08-21 15:00:18.370:E/AndroidRuntime(25252):在 android.view.LayoutInflater.createView(LayoutInflater.java:600) 08-21 15:00:18.370: E/AndroidRuntime(25252): ... 28 更多

08-21 15:00:18.370: E/AndroidRuntime(25252): 由: java.lang.OutOfMemoryError

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.content.res.Resources.loadDrawable(Resources.java:2988)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.content.res.TypedArray.getDrawable(TypedArray.java:602) 08-21 15:00:18.370:E/AndroidRuntime(25252):在 android.widget.ImageView。 (ImageView.java:131)

08-21 15:00:18.370: E/AndroidRuntime(25252): 在 android.widget.ImageView.(ImageView.java:121)

【问题讨论】:

    标签: android out-of-memory viewflipper bitmapfactory android-bitmap


    【解决方案1】:

    尝试使用 Universal Image Loader 或 Picasso,这些库将帮助您解决 OutOfmemory 运行时异常,请按照以下步骤操作(使用 Picasso):

    1- 从这里 (http://square.github.io/picasso/) 下载 picasso jar 文件。

    2- 将 jar 复制到项目的 lib 文件夹中。

    3- 现在右键单击您的 jar 文件并将其添加到构建路径。

    4- 现在在您的 java src 文件中 - 在您的 findviewById(R.id.iv12) 行之后,删除所有与位图相关的代码(例如,删除所有这些 - mCurrentBitmap 、 BitmapFactory.Options mBitmapOptions 、 iv1.setImageBitmap(mCurrentBitmap); ) 同时从您的 xml 文件中删除所有 src(例如 android:src="@drawable/kumana8")。

    5- 改为使用:Picasso.with(context).load(R.drawable.kumana8).into(imageView1); 用于以编程方式将可绘制对象设置为您的图像视图。

    6- 也可以尝试在项目的 android 清单文件中添加 android:largeHeapSize="true"。

    您也可以从这里 (https://github.com/nostra13/Android-Universal-Image-Loader) 或其他图像加载库尝试通用图像加载器。

    【讨论】:

    • 我这样做了。有用。但有时它不会将所有图像加载到图像视图中。任何解决方案
    • Thurul,检查此链接 - stackoverflow.com/questions/25114045/…
    • 您好再次非常感谢您的回复。我已经这样做了。但有时它不会加载一些图像。这是我的代码。你能检查一下是否有什么要改变的。提前致谢。 stackoverflow.com/questions/25503405/…
    • 毕加索是否出现此问题,您是否尝试过其他第 3 部分库。
    • 是的。不,我没有。由于我正在从 Drawable 文件夹加载图像,我认为 Volley 无法帮助它。
    【解决方案2】:

    您将大位图加载到您的应用程序中。尝试减小位图大小或添加

    android:largeHeapSize="true"
    

    进入你的清单。

    【讨论】:

    • 在Manifest中,应该添加到哪里?修改代码有什么建议吗?
    【解决方案3】:

    我认为您的图像尺寸很大。您是否已经使用 1 个或 2 个图像运行?如果您可以使用 1 或 2 个图像运行,则错误取决于图像。您需要减小图像大小。只是我的想法。希望得到帮助。 Compress PNG image tool

    【讨论】:

    • 感谢您的评论。是的,它在一定程度上有所帮助。但这是一个临时解决方案。
    • 您应该尝试第三方延迟加载库。他们会在图像加载时进行内存管理。
    【解决方案4】:

    请仔细阅读,这将解决您的问题。

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

    【讨论】:

      【解决方案5】:

      首先你需要决定哪些尺寸应该有图像。 对于我的示例尺寸是 1920x1080: 对于地图标记,您可以使用 100x100 位图。

      BitmapFactory.Options o = new BitmapFactory.Options();
                              o.inJustDecodeBounds = true;
                              BitmapFactory.decodeFile(filePath);
                              int srcWidth = o.outWidth;
                              int srcHeight = o.outHeight;
                              int dstWidth = 1920;
                              int dstHeight = 1080;
                              int inSampleSize = 1;
                              if (srcHeight > dstHeight || srcWidth > dstWidth) {
                                  final int halfHeight = srcHeight / 2;
                                  final int halfWidth = srcWidth / 2;
                                  while ((halfHeight / inSampleSize) > dstWidth
                                          && (halfWidth / inSampleSize) > dstWidth) {
                                      inSampleSize *= 2;
                                  }
                              }
                              o.inSampleSize = inSampleSize;
                              o.inJustDecodeBounds = false;
                              Bitmap bitmap = BitmapFactory.decodeFile(filePath, o);
      

      你也可以使用 Bitmap.recycle() 来释放内存。

      【讨论】:

        猜你喜欢
        • 2015-01-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多