Android Studio - Activity 图像使模拟器崩溃但在 phone 中工作

Android Studio - Activity with image crashes emulator but works in phone

我在我的 Android 工作室中添加了一个新的 Activity。这个Activity有图。这是我的 xml.file

的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/auxguide1"
tools:context=".AuxGuide1">

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/imageView2"
    android:src="@drawable/logoauxguide"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

这是我的 java class 代码 包裹 com.example.gonalo.meu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

public class AuxGuide1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auxguide1);
        int secondsDelayed = 1;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(AuxGuide1.this, MainActivity.class));

                finish();
            }
        }, secondsDelayed * 4000);
    }

}

它在我的 phone 中完美运行,但是当我 运行 在模拟器上它显示 "sorry the application has stopped"

这是我的logcat

03-28 08:01:20.513 29031-29031/com.example.gonalo.meu W/art: Throwing OutOfMemoryError "Failed to allocate a 79051908 byte allocation with 1048576 free bytes and 63MB until OOM"
03-28 08:01:20.514 29031-29031/com.example.gonalo.meu D/skia: --- allocation failed for scaled bitmap
03-28 08:01:20.514 29031-29031/com.example.gonalo.meu D/AndroidRuntime: Shutting down VM
03-28 08:01:20.515 29031-29031/com.example.gonalo.meu E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process:   com.example.gonalo.meu, PID: 29031
                                                                     java.lang.OutOfMemoryError: Failed to allocate a 79051908 byte allocation with 1048576 free bytes and 63MB until OOM
                                                                        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                        at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                        at android.widget.ImageView.<init>(ImageView.java:152)
                                                                        at android.widget.ImageView.<init>(ImageView.java:140)
                                                                        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1031)
                                                                        at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                        at com.example.gonalo.meu.AuxGuide1.onCreate(AuxGuide1.java:13)
                                                                        at android.app.Activity.performCreate(Activity.java:6237)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-28 08:01:25.402 29031-29031/? I/Process: Sending signal. PID: 29031 SIG: 9

它不会停止我在项目上的工作,但了解为什么会这样对我很有用。将不胜感激帮助。

如果 android 设备必须显示图像,它必须将整个图像加载到内存中。在您的情况下 logoauxguide 是高分辨率图像,因此整个图像将不适合设备内存。因此 OutOfMemoryError 会发生。

但是内存更大的设备,在这种情况下,您的 phone 将能够将图像加载到内存中。

如果要显示图像,必须在任何图像编辑工具中降低其分辨率(640x480 就可以)。此外,您不希望更高的内存映像增加您的 apk 大小:)