android 内存不足图像按钮
android out of memory imagebutton
好的,首先是我正在做的事情,我正在 Android Studio 2.2.3 中构建一个 Android 应用程序,我正在使用意图、各种布局和 imageButtons。
图像
问题
当我 运行 应用程序时,它崩溃说 运行 内存不足但数量太高,这是我的确切错误:
java.lang.OutOfMemoryError:无法分配 33177612 字节的分配,其中有 16767840 个可用字节和 25MB,直到 OOM
我试过的
所以我认为也许降低图像的分辨率会有所帮助,因此我开始将它们从 1080*1920 分辨率降级到 512*512,但问题仍然存在,内存量为 33177612由于某种原因,字节数保持不变。
正如您在第一张图片中看到的,所有 35 张图片只有 1.74 MB,最大的一张为 121KB
我已将 PNG 图像拖放到可绘制文件夹中,以达到其完整的 1080x1920 分辨率
这是它崩溃的第一个布局的样子,您可以在 link 的高清图像中看到它。
几乎解决方案
我右键单击了可绘制文件夹并添加了图标,因此它们会自动添加 hdpi、xhdpi、xxdpi 和 xxxhdpi,这是它停止崩溃的方式,但质量下降到一个不可读的 imageButton 和一个糟糕的外观和不完整您可以在 SD 图像中看到的背景
代码
我不认为代码有问题,因为它编译并且错误是 OOM,但无论如何它都会出现:
package com.example.andres.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class Inicio extends AppCompatActivity implements View.OnClickListener {
ImageButton bEntrar;
ImageButton bLito;
ImageButton bChincho;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inicio);
bEntrar = (ImageButton)findViewById(R.id.bEntrar);
bEntrar.setOnClickListener(this);
bLito = (ImageButton)findViewById(R.id.bLito);
bLito.setOnClickListener(this);
bChincho = (ImageButton)findViewById(R.id.bChincho);
bChincho.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch ((v.getId())){
case R.id.bEntrar:
Intent intent = new Intent(Inicio.this, Menu.class);
startActivity(intent);
break;
case R.id.bLito:
Intent intent1 = new Intent(Inicio.this, Lito.class);
startActivity(intent1);
break;
case R.id.bChincho:
Intent intent2 = new Intent(Inicio.this, Chincho.class);
startActivity(intent2);
break;
}
}
}
最后是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andres.myapplication">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/logo1"
android:label="Chincho y Lito"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Inicio">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Video">
</activity>
<activity android:name=".Lito">
</activity>
<activity android:name=".Chincho">
</activity>
<activity android:name=".Informacion">
</activity>
<activity android:name=".Rutina">
</activity>
<activity android:name=".Menu">
</activity>
<activity android:name=".Menu2">
</activity>
<activity android:name=".Menu3">
</activity>
<activity android:name=".Tips">
</activity>
</application>
</manifest>
如果您需要我展示任何其他内容,请务必询问我,在此先感谢您。
早期 android 设备的 per-app 上限为 16MB,因此您永远不应超过该上限以支持所有 android 设备。你的图片对于那个上限来说太大了。我建议您为背景使用压缩质量的较小 JPEG,并为图像使用固定的 PNG 大小。确定您的图像在 DP 中的大小,然后使用 this 等工具创建像素大小的图像以支持所有屏幕密度。
同时考虑为您的按钮使用 9patch 图像。 9patch 在支持所有按钮尺寸的同时减小了整体图像尺寸。
https://developer.android.com/studio/write/draw9patch.html
好的,首先是我正在做的事情,我正在 Android Studio 2.2.3 中构建一个 Android 应用程序,我正在使用意图、各种布局和 imageButtons。
图像
问题
当我 运行 应用程序时,它崩溃说 运行 内存不足但数量太高,这是我的确切错误:
java.lang.OutOfMemoryError:无法分配 33177612 字节的分配,其中有 16767840 个可用字节和 25MB,直到 OOM
我试过的
所以我认为也许降低图像的分辨率会有所帮助,因此我开始将它们从 1080*1920 分辨率降级到 512*512,但问题仍然存在,内存量为 33177612由于某种原因,字节数保持不变。
正如您在第一张图片中看到的,所有 35 张图片只有 1.74 MB,最大的一张为 121KB
我已将 PNG 图像拖放到可绘制文件夹中,以达到其完整的 1080x1920 分辨率
这是它崩溃的第一个布局的样子,您可以在 link 的高清图像中看到它。
几乎解决方案
我右键单击了可绘制文件夹并添加了图标,因此它们会自动添加 hdpi、xhdpi、xxdpi 和 xxxhdpi,这是它停止崩溃的方式,但质量下降到一个不可读的 imageButton 和一个糟糕的外观和不完整您可以在 SD 图像中看到的背景
代码
我不认为代码有问题,因为它编译并且错误是 OOM,但无论如何它都会出现:
package com.example.andres.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class Inicio extends AppCompatActivity implements View.OnClickListener {
ImageButton bEntrar;
ImageButton bLito;
ImageButton bChincho;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inicio);
bEntrar = (ImageButton)findViewById(R.id.bEntrar);
bEntrar.setOnClickListener(this);
bLito = (ImageButton)findViewById(R.id.bLito);
bLito.setOnClickListener(this);
bChincho = (ImageButton)findViewById(R.id.bChincho);
bChincho.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch ((v.getId())){
case R.id.bEntrar:
Intent intent = new Intent(Inicio.this, Menu.class);
startActivity(intent);
break;
case R.id.bLito:
Intent intent1 = new Intent(Inicio.this, Lito.class);
startActivity(intent1);
break;
case R.id.bChincho:
Intent intent2 = new Intent(Inicio.this, Chincho.class);
startActivity(intent2);
break;
}
}
}
最后是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andres.myapplication">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/logo1"
android:label="Chincho y Lito"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Inicio">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Video">
</activity>
<activity android:name=".Lito">
</activity>
<activity android:name=".Chincho">
</activity>
<activity android:name=".Informacion">
</activity>
<activity android:name=".Rutina">
</activity>
<activity android:name=".Menu">
</activity>
<activity android:name=".Menu2">
</activity>
<activity android:name=".Menu3">
</activity>
<activity android:name=".Tips">
</activity>
</application>
</manifest>
如果您需要我展示任何其他内容,请务必询问我,在此先感谢您。
早期 android 设备的 per-app 上限为 16MB,因此您永远不应超过该上限以支持所有 android 设备。你的图片对于那个上限来说太大了。我建议您为背景使用压缩质量的较小 JPEG,并为图像使用固定的 PNG 大小。确定您的图像在 DP 中的大小,然后使用 this 等工具创建像素大小的图像以支持所有屏幕密度。
同时考虑为您的按钮使用 9patch 图像。 9patch 在支持所有按钮尺寸的同时减小了整体图像尺寸。 https://developer.android.com/studio/write/draw9patch.html