2014-01-29 124 views
0

我抓到了一個代碼示例來捏和縮放圖像。作品確定。問題是當我在我的應用程序中實現此代碼時。我在這裏有一個OutOfMemoryError:Android:OutOfMemoryError加載圖像資源

view.setImageResource(R.drawable.planometro);

爲了解決這個問題,我發現這個代碼:

public Drawable getAssetImage (String filename) throws IOException { 

    AssetManager assets = getResources() getAssets().; 
    InputStream buffer = new BufferedInputStream ((assets.open ("drawable /" + filename +)) "png."); 
    Bitmap bitmap = BitmapFactory.decodeStream (buffer); 
    return new BitmapDrawable (getResources(), bitmap); 
} 

的問題是,這個代碼是返回一個int,而不是繪製。我如何解決我的錯誤?

感謝

+1

縮小你的位圖 –

+0

我該怎麼辦?碼? – user3231711

+0

可能是你的圖片有很高的分辨率 – Piyush

回答

0

試試這個

try { 
    Bitmap bitmap=((BitmapDrawable)activity.getResources().getDrawable(R.drawable.matches_placeholder_upcoming2x)).getBitmap(); 
    Drawable drawable = new BitmapDrawable(activity.getResources(),bitmap); 
    liveMatchHeaderContainer.setBackgroundDrawable(drawable);  

} catch (OutOfMemoryError E) { 
    E.printStackTrace(); 
} 
+0

如果我做一個backgroundDrawable,我的應用程序不工作,因爲我想捏圖像放大 – user3231711

0

這爲我工作,嘗試一下:

 ImageView image = (ImageView) findViewById(R.id.img); 
     try { 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.ic_launcher); 
//   Drawable d = new BitmapDrawable(getResources(), bitmap); 
      image.setBackgroundResource(R.drawable.ic_launcher); 

     } catch (OutOfMemoryError E) { 
      E.printStackTrace(); 
     } 
+0

此代碼不起作用 – user3231711

+0

你使用什麼項目生成目標api? –