gpt4 book ai didi

android - 如何在 Android 中增加内存的清晰解释?

转载 作者:行者123 更新时间:2023-11-29 01:13:10 25 4
gpt4 key购买 nike

我的图像使用 150 DPI。在我看来,它们真的没有那么大,而且是一个相当大的项目。如何增加 Android 应用程序的虚拟内存?

这是控制台中发生的事情:

W/art: Throwing OutOfMemoryError "Failed to allocate a 409534333 byte allocation with 9927994 free bytes and 177MB until OOM"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.com.example.android.brainboost, PID: 3817
java.lang.OutOfMemoryError: Failed to allocate a 409534333 byte allocation with 9927994 free bytes and 177MB until OOM
at com.android.tools.fd.runtime.FileManager.readRawBytes(FileManager.java:831)
at com.android.tools.fd.runtime.FileManager.checkInbox(FileManager.java:249)
at com.android.tools.fd.runtime.BootstrapApplication.createResources(BootstrapApplication.java:126)
at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:235)
at android.app.Application.attach(Application.java:187)
at android.app.Instrumentation.newApplication(Instrumentation.java:1020)
at android.app.Instrumentation.newApplication(Instrumentation.java:1004)
at android.app.LoadedApk.makeApplication(LoadedApk.java:573)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4749)
at android.app.ActivityThread.access$1600(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

这是我的代码:

package app.com.example.android.brainboost;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;

public class delta extends AppCompatActivity {

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

MediaPlayer mp3;
public void level_eight(View view){

mp3 = MediaPlayer.create(this, R.raw.d_zero_five);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_seven(View view){

mp3 = MediaPlayer.create(this, R.raw.d_one);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_six(View view){

mp3 = MediaPlayer.create(this, R.raw.d_one_five);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_five(View view){

mp3 = MediaPlayer.create(this, R.raw.d_two);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_four(View view){

mp3 = MediaPlayer.create(this, R.raw.d_two_five);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_three(View view){

mp3 = MediaPlayer.create(this, R.raw.d_three);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_two(View view){

mp3 = MediaPlayer.create(this, R.raw.d_three_five);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public void level_one(View view){

mp3 = MediaPlayer.create(this, R.raw.d_four);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.pause();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
mp3.start();
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.pause();
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_delta"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.com.example.android.brainboost.delta"
android:background="#FFFFFF">

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_8"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/lv8"
android:onClick="level_eight" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_7"
android:layout_below="@+id/lv8"
android:layout_centerHorizontal="true"
android:id="@+id/lv7"
android:onClick="level_seven" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_6"
android:id="@+id/lv6"
android:layout_below="@+id/lv7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="level_six" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_5"
android:layout_below="@+id/lv6"
android:layout_centerHorizontal="true"
android:id="@+id/lv5"
android:onClick="level_five" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_4"
android:layout_below="@+id/lv5"
android:layout_centerHorizontal="true"
android:id="@+id/lv4"
android:onClick="level_four" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_3"
android:id="@+id/lv3"
android:layout_below="@+id/lv4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="level_three" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_2"
android:id="@+id/lv2"
android:layout_below="@+id/lv3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="level_two" />

<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="@drawable/dlv_1"
android:id="@+id/lv1"
android:onClick="level_one"
android:layout_below="@+id/lv2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageView
app:srcCompat="@drawable/logo"
android:id="@+id/logo"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

最佳答案

增加应用程序使用的内存的唯一方法是设置android:largeHeap="true" 在 Manifest 的 application 标签中。显然设置这个有一些缺点,你可以阅读 here .

如果您收到 OOM (OutOfMemory) 错误,99% 是您在处理应用程序内存方面的错误。如果您需要读取一个大文件(可能是因为您需要解析内容),请不要将其直接加载到 RAM 中,而是一次以固定的字节 block 读取它。

在没有看到您的代码或不知道您的 Intent 的情况下,我们不会更有用

要加快 Android Studio 的速度,您可以增加专用于 JVM 的 RAM。阅读此处了解更多信息 herehere

关于android - 如何在 Android 中增加内存的清晰解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com