2015-10-04 88 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