2013-12-10 38 views
0

我得到了logcat的錯誤:如何修復android中的「java.lang.OutOfMemoryError」?

12-10 17:49:21.393: E/AndroidRuntime(16350): FATAL EXCEPTION: main 
12-10 17:49:21.393: E/AndroidRuntime(16350): java.lang.OutOfMemoryError 
12-10 17:49:21.393: E/AndroidRuntime(16350): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
12-10 17:49:21.393: E/AndroidRuntime(16350): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527) 
12-10 17:49:21.393: E/AndroidRuntime(16350): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301) 
12-10 17:49:21.393: E/AndroidRuntime(16350): at com.smalt.photoview.MyAdapter.getView(MyAdapter.java:110) 

我的Android類文件這樣寫:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder viewHolder = new ViewHolder(); 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.item, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.imageView = (ImageView) convertView 
       .findViewById(R.id.item_imageview); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 
     Bitmap bitmap = BitmapFactory.decodeFile(
       imgPathlist.get(position % imgPathlist.size()), options); 
     viewHolder.imageView.setImageBitmap(bitmap);// 爲ImageView設置內容 
     viewHolder.imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     viewHolder.imageView.setLayoutParams(new LinearLayout.LayoutParams(
       100, 100)); 
     viewHolder.imageView.setBackgroundResource(mGalleryItemBackground); 
     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    return convertView; 
} 


class ViewHolder { 
    ImageView imageView; 
} 

我不知道如何解決的代碼,請教我該怎麼辦?

回答

1

它發生是因爲圖像大小。你必須調整或壓縮圖像。下面的代碼將幫助你。使用按您需要:

public static Bitmap loadResizedBitmap(String filename, int width, int height, boolean exact) { 
    Bitmap bitmap = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filename, options); 
    if (options.outHeight > 0 && options.outWidth > 0) { 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = 2; 
     while ( options.outWidth/options.inSampleSize > width 
       && options.outHeight/options.inSampleSize > height) { 
      options.inSampleSize++; 
     } 
     options.inSampleSize--; 

     bitmap = BitmapFactory.decodeFile(filename, options); 
     if (bitmap != null && exact) { 
      bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); 
     } 
    } 
    return bitmap; 
} 

OR

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 
1

我懷疑壓縮不會幫助,它會解壓縮和碰撞;你必須使用更小的圖像。或者,可能是漸變背景。

相關問題