Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:加载图像时出错:OutOfMemoryError:位图大小超出VM预算_Java_Android_Android Layout_Tablet - Fatal编程技术网

Java Android:加载图像时出错:OutOfMemoryError:位图大小超出VM预算

Java Android:加载图像时出错:OutOfMemoryError:位图大小超出VM预算,java,android,android-layout,tablet,Java,Android,Android Layout,Tablet,我正在为平板电脑(1024x768)开发一个Android应用程序。我在这里搜索并找到了一些解决方案,但任何人都解决了我的问题。该应用程序有5个屏幕,每个屏幕都有一个我加载的ImageView: <ImageView android:id="@+id/imageView1" android:layout_width="1024dp" android:layout_height="768dp" android:layout_centerHorizontal="

我正在为平板电脑(1024x768)开发一个Android应用程序。我在这里搜索并找到了一些解决方案,但任何人都解决了我的问题。该应用程序有5个屏幕,每个屏幕都有一个我加载的ImageView:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="1024dp"
    android:layout_height="768dp"
    android:layout_centerHorizontal="TRUE"
    android:src="@drawable/scene1"
    />

@覆盖
受保护的空onDestroy()
{
super.ondestory();
不可绑定的drawables(findviewbyd(R.id.RootView));
gc();
}
私有void未绑定可提取项(视图)
{
if(view.getBackground()!=null)
{
view.getBackground().setCallback(null);
}
if(视图组的视图实例)
{
对于(int i=0;i<((视图组)视图)。getChildCount();i++)
{
未绑定的Drawables(((视图组)视图).getChildAt(i));
}
((视图组)视图);
}
}
但我总是犯错误

图像为PNG,其权重小于50k

有什么建议吗


谢谢

首先,调用System.gc()并不会真正让垃圾收集器通过,而且它会真正影响性能,因此我会限制自己广泛使用它。如果希望垃圾收集器清理图像,请改用WeakReferences,并将图像存储在HashMap上


其次,尽量使用较小的图像,因为虚拟机将很快崩溃。

1024x768是DP中的尺寸,像素数可能是高分辨率设备上的两倍

如果使用bitmapfactory解码1024x768图像,它将根据密度缩放位图,因此最终可能会得到2048x1536图像,这将占用大约12MB的空间


如果您总是希望图像具有相同的分辨率,而不考虑设备分辨率,则将其放入
Drawable nodpi
文件夹,这将导致解码时不会调整其大小。

尝试使用onCreate()方法加载图像,而不是从XML加载图像

您是否尝试过先读取图像的尺寸和类型,然后加载该图像的缩小版本


正如建议的那样。

图像的文件大小几乎不相关。以像素为单位的宽度和高度是多少?1024x768是宽度和高度,但我需要图像来填充整个屏幕…就是这样。我使用了:public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){//First decode with inJustDecodeBounds=true以检查维度最终位图工厂。Options Options=new BitmapFactory.Options();Options.inJustDecodeBounds=true;BitmapFactory.decodeResource(res,resId,Options);//计算inSampleSize选项。inSampleSize=calculateInSampleSize(选项,reqWidth,reqHeight);//使用inSampleSize设置选项解码位图。InBustDecodeBounds=false;返回BitmapFactory.decodeResource(res,resId,options);}
05-28 17:03:51.774: E/AndroidRuntime(401): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>

05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>


05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: java.lang.reflect.InvocationTargetException


05-28 17:03:51.774: E/AndroidRuntime(401): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 @Override
  public void onPause()
  {
    super.onPause();
    Log.i("URI", "Syste.gc");
    // yada yada yada...

    System.gc();
  }
@Override
protected void onDestroy()
{
    super.onDestroy();

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
}

private void unbindDrawables(View view)
{
    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();
    }
}