如何在android中修复“java.lang.OutOfMemoryError”?

时间:2013-12-10 09:59:58

标签: android

我收到了logcat错误:

 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;
}

我不知道如何修复代码,请教我怎么做?

2 个答案:

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

我怀疑压缩不会有帮助,它会解压缩并崩溃;你必须使用较小的图像。或者,可能是渐变背景。