我正在为我的图像使用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>
答案 0 :(得分:1)
增加应用程序使用的内存的唯一方法是设置
清单的应用程序标记中的android:largeHeap="true"
。
显然设置这个有一些缺点,你可以阅读here。
如果您收到OOM(OutOfMemory)错误,99%是您处理应用内存的错误。 如果你需要阅读一个大文件(可能是因为你需要解析内容),请不要将它直接加载到RAM中,而是一次以固定的字节块读取它。
如果没有看到您的代码或者不了解您的意图,我们就无法提供更多有用的信息
答案 1 :(得分:0)
我通过改变&#34; match-parent&#34;中的图像来解决问题。他们适当的dp。