2015-10-04 81 views
0

我是新来的Android以下this在Android中创建一个MEME应用程序的教程一切都工作正常,直到我添加图像到我的RelativeLayout背景的一个fragement。OutOfMemory异常布局的背景图像

<?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。但everycase我来了跨使用ImageView,但我有一个布局的背景图像,而不是一个ImageView,所以我怎么才能从布局获取ImageView对象,以便我可以使用。

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

感谢

+0

您使用的图像的像素大小是多少? –

+0

大小是'262kb'我知道如果我使用小尺寸图片的概率可能会解决,但我怎样才能处理它与像素的代码'1790 x 1757' – codegasmer

+0

但是,像素的大小是多少(图像的尺寸) ? –

回答

1

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

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

如果它不尝试drawable-nodpi。

+0

thaku你非常非常用'drawable-xxhdpi'用心感谢你所有的努力,这意味着很多我努力这是几天,这很容易再次感谢你。你可以解释为什么改变文件夹的工作我只是一个初学者在这 – codegasmer

+0

因为你尝试的设备是xxhdpi,并且默认的基础Android密度是mdpi,所以导致OS对已经非常大的图像进行重大调整。 –

0
First get the bitmap 
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100); 

Convert the bitmap to drawable as follows 
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap); 

Finally you can use mDrawable to set your relative layout background 
+0

另一个使用解决方案为我工作,但我也尝试你的解决方案我做'layout.setBackgroundDrawable(mDrawable) ;'但这是depricated。我是新来的这个,你可以帮助 – codegasmer

+0

setbackground工作在API级别16和以下setbackgrounddrawabale – USKMobility