Android 取消绑定drawables onPause()会导致无响应的返回导航并跳过此步骤会导致内存溢出

Android 取消绑定drawables onPause()会导致无响应的返回导航并跳过此步骤会导致内存溢出,android,android-layout,android-image,android-memory,Android,Android Layout,Android Image,Android Memory,我正在使用图像设置为我所有活动的背景,但它导致内存溢出问题并导致应用程序崩溃。现在,我在活动中的暂停()和销毁()上解除了可拖动文件的绑定,现在按下后退按钮时显示空白屏幕。那么,我如何在不使用额外内存的情况下避免这种情况呢 protected void onPause(){ super.onPause(); unbindDrawables(findViewById(R.id.login_root)); } protected void onDestroy() {

我正在使用图像设置为我所有活动的背景,但它导致内存溢出问题并导致应用程序崩溃。现在,我在活动中的暂停()和销毁()上解除了可拖动文件的绑定,现在按下后退按钮时显示空白屏幕。那么,我如何在不使用额外内存的情况下避免这种情况呢

    protected void onPause(){
    super.onPause();
    unbindDrawables(findViewById(R.id.login_root));
}

protected void onDestroy() {
        unbindDrawables(findViewById(R.id.login_root));
        super.onDestroy();
      }

private void unbindDrawables(View view) {
    System.gc();
    Runtime.getRuntime().gc();
    if (view.getBackground() != null) {
    view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
        unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
    ((ViewGroup) view).removeAllViews();
    }
protectedvoid onPause(){
super.onPause();
不绑定的drawables(findviewbyd(R.id.login_root));
}
受保护的空onDestroy(){
不绑定的drawables(findviewbyd(R.id.login_root));
super.ondestory();
}
私有void未绑定可提取项(视图){
gc();
Runtime.getRuntime().gc();
if(view.getBackground()!=null){
view.getBackground().setCallback(null);
}
if(视图组的视图实例){
对于(int i=0;i<((视图组)视图)。getChildCount();i++){
未绑定的Drawables(((视图组)视图).getChildAt(i));
}
((视图组)视图);
}
起初,我使用android:background=“@drawable/”扩展我的布局,这总是导致内存溢出错误,说VM不允许我们分配10MB(应用)。现在,我从该可绘制设备获取位图,而不进行缩放并在运行时绑定。现在,它说VM不允许我们分配5MB(应用),而不使用不可绑定的可绘制设备(…) 很明显,显示的背景图像质量有所下降,但我无法理解,如果我使用的是13KB的png文件,JVM如何需要5或10MB的空间来处理请求

我已将布局语句从onCreate()转换为onResume()方法,但按下“上一步”按钮后,应用程序的内存再次耗尽

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

  protected void onResume(){
        setContentView(R.layout.home);
        Bitmap bmp;
        ImageView background = (ImageView)findViewById(R.id.iv_home_background);
        InputStream is = getResources().openRawResource(R.drawable.background);
        bmp = BitmapFactory.decodeStream(is);
        background.setImageBitmap(bmp);

         super.onResume();
    }

 protected void onPause(){
        super.onPause();
        unbindDrawables(findViewById(R.id.home_root));
    }


 private void unbindDrawables(View view) {
        System.gc();
        Runtime.getRuntime().gc();
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        }
    }
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
受保护的void onResume(){
setContentView(R.layout.home);
位图bmp;
ImageView背景=(ImageView)findViewById(R.id.iv_home_background);
InputStream is=getResources().openRawResource(R.drawable.background);
bmp=位图工厂.decodeStream(is);
背景设置图像位图(bmp);
super.onResume();
}
受保护的void onPause(){
super.onPause();
不可绑定的drawables(findviewbyd(R.id.home_root));
}
私有void未绑定可提取项(视图){
gc();
Runtime.getRuntime().gc();
if(view.getBackground()!=null){
view.getBackground().setCallback(null);
}
if(视图组的视图实例){
对于(int i=0;i<((视图组)视图)。getChildCount();i++){
未绑定的Drawables(((视图组)视图).getChildAt(i));
}
((视图组)视图);
}
}

我找到了解决此问题的方法。现在我正在运行时将位图缩放到非常小的大小,然后将其存储在内部存储器中。程序在运行时从存储器调用缩放后的位图,如果不存在,则从drawable文件夹调用它,缩放它,将其写入存储器,然后将其绑定到视图。 这样,在任何时候都不需要调用unbindDrawables方法,应用程序始终保持响应。
我现在唯一关心的是位图的质量,我认为我需要调整缩放大小,以找出具有最高质量的最小可能大小。

您正在为每个子视图调用GC。请在所有取消绑定完成后仅尝试调用一次

unbindDrawables(findViewById(R.id.login_root));
System.gc();
GC是一个很重的负载,太频繁地调用它是没有用的。事实上,如果没有泄漏,它应该是不安全的

还要记住,png文件的大小与内存中的位图无关

请记住,如果调用了onDestroy(),那么onPause()之前也会被调用。如@JohnSatriano所述,仅在
onPause()中解除绑定。
但是,一定要向我们展示你的onResume函数。我没有覆盖我的onResume函数。我完全不知道该怎么做。我在onResume()中做了更改代码,但它会使我的应用程序崩溃。PNG图像是压缩的,但要显示,系统必须将其保存在内存中未压缩,因此它需要3个字节或(透明)每像素4个字节。对于640x480像素的图像,这是1.2MB。