1

嗨,我在这里做一个应用程序,我正在使用更多图像,在数组列表中我有图像我将这些数组列表转换为数组,然后我将该字符串数组添加到 imageview 但是当活动开始时,我得到的位图大小超过异常。只有一些有时我面临这个问题,但有时它运行良好。有时只有我面临这个问题...我尝试了很多方法但没有结果。我在我的代码下面提到任何有想法的人请建议我。

 LetterGameActivity .class:

 public class LetterGameActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

           birdimg1 = (ImageView) findViewById(R.id.birdimg);
        option11 = (TextView)findViewById(R.id.opt1);
        option12 = (TextView)findViewById(R.id.opt2);
        option13 = (TextView)findViewById(R.id.opt3);

                        option11.setText(stringArraynew[0]);
            option12.setText(stringArraynew1[0]);
            option13.setText(stringArraynew2[0]);


             BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getResources(), stringArraynew3[0], options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            String imageType = options.outMimeType;

            birdimg1.setImageBitmap(
                    decodeSampledBitmapFromResource(getResources(), stringArraynew3[0], 100, 100));
}

   public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

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) {
    if (width > height) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    } else {
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }
}
return inSampleSize;
  }
 protected void onPause() {
    super.onPause();
System.gc();
   }
protected void onDestroy()
{
    super.onDestroy();
       System.gc();
    }

    }
4

0 回答 0