1

我的 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();
}

这是我的 LogCat ...

    07-12 15:21:11.465: E/AndroidRuntime(23419): FATAL EXCEPTION: main

07-12 15:21:11.465: E/AndroidRuntime(23419): java.lang.OutOfMemoryError

07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.Bitmap.nativeCreate(Native Method)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:468)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at com.done.SS.onCreate(SS.java:65)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.Activity.performCreate(Activity.java:4562)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.os.Looper.loop(Looper.java:137)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at android.app.ActivityThread.main(ActivityThread.java:4514)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at java.lang.reflect.Method.invokeNative(Native Method)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at java.lang.reflect.Method.invoke(Method.java:511)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-12 15:21:11.465: E/AndroidRuntime(23419):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

你需要它们的全尺寸吗?尝试设置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();
 }
于 2012-07-12T12:43:56.813 回答