java - 布局背景图像的 OutOfMemory 异常

标签 java android image memory bitmap

我是 Android 新手,关注 this在 Android 中创建 MEME 应用程序的教程一切都工作正常,直到我将图像添加到 fragment 的相对布局背景中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/dp"
    android:id="@+id/imageOut">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/topText"
        android:id="@+id/topMemeText"
        android:layout_marginTop="32dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/bottomText"
        android:id="@+id/bottomMemeText"
        android:layout_marginBottom="69dp"
        android:textColor="#FFF"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

错误是

 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 86687164 byte allocation with 1048576 free bytes and 63MB until OOM

我知道这个问题是由于图像大小造成的,我必须对其进行解码,因为我在网上和这里搜索了很多情况http://developer.android.com/training/displaying-bitmaps/load-bitmap.html但是我遇到的每种情况都使用 ImageView,但我有布局的背景图像而不是 ImageView,所以我如何从布局中获取 ImageView 对象以便我可以使用。

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

谢谢

最佳答案

我的猜测是图像大小的调整导致了您的问题。

尝试将图像添加到drawable-xxhdpi,看看是否有帮助。

如果没有尝试drawable-nodpi。

关于java - 布局背景图像的 OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32931934/

相关文章:

java - 将文件从 windows Java 应用程序移动到 linux box

java - 如何将 double 方法设置为等于静态方法中的值?

Android - 向上滑动面板(库)圆角

java - 如何在 Android 中为按钮文本使用两种不同的字体大小?

c# - 我怎样才能返回图像?

css - 图像缩放和动画改进

java - 是否可以通过在eclipse中下载源文件夹来运行程序?

Android 卡在 "Installing"和 "Launching App"

android - 带有图像和文本的按钮,放置图像的方式

java - 如何使用 JDO 在 AppEngine 中的多对一中按父实体过滤实体