Android 使用位图图像生成OutOfMemoryError

Android 使用位图图像生成OutOfMemoryError,android,android-widget,out-of-memory,Android,Android Widget,Out Of Memory,我的Android应用程序中有一个活动,试图使用17幅图像作为位图。但是,当我尝试启动此活动时,它返回java.lang.OutOfMemoryError。我真的解决不了这个问题。谁能给我一个确切的答案?下面是我的onCreate()。多谢各位 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.seviyo

我的Android应用程序中有一个活动,试图使用17幅图像作为位图。但是,当我尝试启动此活动时,它返回
java.lang.OutOfMemoryError
。我真的解决不了这个问题。谁能给我一个确切的答案?下面是我的
onCreate()
。多谢各位

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.seviyor);

    activityHead=(ImageView) findViewById(R.id.activityhead1);
    shake = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    Options options=new Options();
    options.inSampleSize=1;
    for(int i=0;i<17;i++){  
        bmImages[i]=BitmapFactory.decodeResource(getResources(),drwbles[i] , options);
    }

    initilizeToastBackground();
    initilizeButtonsFor16Leafs();
    initilize16Lisener();

    resultIntent=new Intent(this,ResultFrame.class);
    fortuneCon=16;
    for(int i=0;i<17;i++){
        leafs[i]=(ImageView) findViewById(leafIDs[i]);
    }
    for(int i=0;i<17;i++){
        leafs[i].setImageBitmap(bmImages[i]);
    }


    Bundle frompreviousInent=getIntent().getExtras();
    categoryName=frompreviousInent.getString("category");
    setHeadOfActivity();
}

你需要全尺寸的吗?尝试设置
options.inSampleSize=8
。或者至少2到4个

我怀疑有17张图片,这可能是不够的

另一种解决方案是查询图像并将其作为缩略图输入到您的活动中:

Uri tUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI; // Where thumbnails are stored
String[] columns = { MediaStore.Images.ImageColumns._ID,
                     MediaStore.Images.ImageColumns.DATE_TAKEN,MediaStore.Images.ImageColumns.DISPLAY_NAME};

Cursor thumbCursor = this.managedQuery(tUri, null, null, null, null); // May need to use more paramaters to get
                                                                      // exactly the images you need
int i = 0;
if (thumbCursor.moveToFirst() && thumbCursor.getCount() == 17) 
{
    do
    {
         int columnIndex = thumbCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
         String filePath = thumbCursor.getString(columnIndex);
         Bitmap bitmap = BitmapFactory.decodeFile(filePath);
         leafs[i]=(ImageView) findViewById(leafIDs[i]);
         leafs[i].setImageBitmap(bitmap);
         i++;
     }
     while(thumbCursor.moveToNext());
     thumbCursor.close();
 }

你需要全尺寸的吗?尝试设置
options.inSampleSize=8
。或者至少2到4个

我怀疑有17张图片,这可能是不够的

另一种解决方案是查询图像并将其作为缩略图输入到您的活动中:

Uri tUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI; // Where thumbnails are stored
String[] columns = { MediaStore.Images.ImageColumns._ID,
                     MediaStore.Images.ImageColumns.DATE_TAKEN,MediaStore.Images.ImageColumns.DISPLAY_NAME};

Cursor thumbCursor = this.managedQuery(tUri, null, null, null, null); // May need to use more paramaters to get
                                                                      // exactly the images you need
int i = 0;
if (thumbCursor.moveToFirst() && thumbCursor.getCount() == 17) 
{
    do
    {
         int columnIndex = thumbCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
         String filePath = thumbCursor.getString(columnIndex);
         Bitmap bitmap = BitmapFactory.decodeFile(filePath);
         leafs[i]=(ImageView) findViewById(leafIDs[i]);
         leafs[i].setImageBitmap(bitmap);
         i++;
     }
     while(thumbCursor.moveToNext());
     thumbCursor.close();
 }

重复:调试您的代码,并在早上开始通信。所以你们应该删除这个问题,否则管理员一定要关闭这个问题。重复:调试你们的代码,并开始和你们早上提出的问题进行交流。所以你应该删除这个问题,否则管理员一定要关闭这个问题