OutOfMemoryError:backgroud drawable - Android

时间:2015-08-14 09:48:38

标签: android view background drawable

救救我! 我用

创建了一个android项目

MainActivity类:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <View
        android:id="@+id/bg_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg_1" />
    <View
        android:id="@+id/bg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_2"/>
    <View
        android:id="@+id/bg_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_3"/>
</RelativeLayout>

和视图activity_main.xml:

Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:514)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:359)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:786)
            at android.content.res.Resources.loadDrawable(Resources.java:1969)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
            at android.view.View.<init>(View.java:3353)
            at android.view.View.<init>(View.java:3290)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:587)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:409)
            at android.app.Activity.setContentView(Activity.java:2087)
            at com.example.hoa.test1.MainActivity.onCreate(MainActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5238)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4904)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)

Drawable文件夹包含三个图片: bg_1.jpg(66k), bg_2.jpg(190k), bg_3.jpg(48k)

看起来很简单,但在运行时会抛出错误:

h3:before {
font-family: 'ElegantIcons';
content: "\35";
float:left;
}

h3.alt:before {
font-family: 'ElegantIcons';
content: "\38";
float:left;
}

有人给我原因和解决方案吗? 非常感谢你!

3 个答案:

答案 0 :(得分:1)

您的图片可能看起来不大,因为它们的大小,但重要的是它的分辨率。您可以自己缩小它们,也可以以编程方式设置图像并尝试使用以下方法:

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(f), null, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE=70;

    // Find the correct scale value. It should be the power of 2.
    int scale = 1;
    while(o.outWidth / scale / 2 >= REQUIRED_SIZE && 
          o.outHeight / scale / 2 >= REQUIRED_SIZE) {
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

这里也很好看: http://developer.android.com/training/displaying-bitmaps/index.html

答案 1 :(得分:1)

在不同的屏幕密度上制作不同的图像尺寸(drawable-hdpi,drawable-xhdpi等)。

答案 2 :(得分:0)

尝试减少图片的大小。看起来你的设备内存不足