Android:OutOfMemoryError加载imageResource

时间:2014-01-29 11:11:08

标签: android image bitmap out-of-memory drawable

我抓住了一个代码示例来捏合和缩放图像。工作还可以。问题是我在我的应用程序中实现此代码。我在这里有一个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,而不是一个drawable。我如何解决我的错误?

由于

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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();
        }