Размер растрового изображения ошибки outofmemory Android превышает бюджет VM в 2.3.3

Я понимаю, что этот вопрос задают несколько раз. Ни один из них не дает ясного решения. Позвольте мне объяснить проблему.

  1. У меня есть Activity, которое загружает 4 изображения за раз.
  2. Я загружаю изображения в метод onResume ().
  3. Действие выдает ошибку растрового изображения при загрузке.

Примечания.

  1. Я устанавливаю изображение с помощью вызова метода setImageResource (R.drawable.xxxx), а не напрямую с растровым изображением / чертежами.
  2. Изображения масштабированы правильно.
  3. Activtiy РАБОТАЕТ ОТЛИЧНО во всех эмуляторах до 2.3 и РАБОТАЕТ ОТЛИЧНО на фактическом устройстве (Samsung Galaxy 5)
  4. Ошибка появляется при первой инициализации, и событие изменения ориентации не запускается.
  5. изображения имеют масштаб 800 x 600 и средний размер 15 КБ (каждое).

Сообщите мне любые решения. Также дайте мне знать, если у вас возникнут аналогичные проблемы с эмулятором Android 2.3.3.

[update] -snippets

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            ...
    img_topLeft = (ImageView) findViewById(R.id.Img_Alph_Q_TopLeft);
    img_topRight = (ImageView) findViewById(R.id.Img_Alph_Q_TopRight);
    img_bottomLeft = (ImageView) findViewById(R.id.Img_Alph_Q_BottomLeft);
    img_bottomRight = (ImageView) findViewById(R.id.Img_Alph_Q_BottomRight);
   ...
   }
protected void onResume() {
    super.onResume();
            img_topLeft.setImageResource(R.drawable.xxx);
            img_topRight.setImageResource(R.drawable.xxx);
            img_bottomLeft.setImageResource(R.drawable.xxx);
            img_bottomRight.setImageResource(R.drawable.xxx);
   ...
   }

03-21 08: 59: 17.362: ОШИБКА / dalvikvm-heap (5883): 4320000 байт внешнего выделения слишком велико для этого процесса. 03-21 08: 59: 17.412: ERROR / GraphicsJNI (5883): виртуальная машина не позволяет нам выделить 4320000 байт 03-21 08: 59: 17.432: ERROR / AndroidRuntime (5883): FATAL EXCEPTION: main 03-21 08: 59: 17.432: ERROR / AndroidRuntime (5883): java.lang.OutOfMemoryError: размер растрового изображения превышает бюджет виртуальной машины 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.Bitmap.nativeCreate (собственный метод) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.Bitmap.createBitmap (Bitmap.java:477) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.Bitmap.createBitmap (Bitmap.java:444) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.Bitmap.createScaledBitmap (Bitmap.java:349) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.BitmapFactory.finishDecode (BitmapFactory.java:498) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:473) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:336) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:697) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): на android.content.res.Resources.loadDrawable (Resources.java:1709) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): на android.content.res.Resources.getDrawable (Resources.java:581) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.widget.ImageView.resolveUri (ImageView.java:501) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.widget.ImageView.setImageResource (ImageView.java:280) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в Quiz.java:124) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): Quiz.onResume (Quiz.java:92) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.Instrumentation.callActivityOnResume (Instrumentation.java:1150) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.Activity.performResume (Activity.java:3832) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread.performResumeActivity (ActivityThread.java:2110) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread.handleResumeActivity (ActivityThread.java:2135) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1668) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread.access 1500 долларов (ActivityThread.java:117) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread $ H.handleMessage (ActivityThread.java:931) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.os.Handler.dispatchMessage (Handler.java:99) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.os.Looper.loop (Looper.java:123) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в android.app.ActivityThread.main (ActivityThread.java:3683) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в java.lang.reflect.Method.invokeNative (собственный метод) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): в java.lang.reflect.Method.invoke (Method.java:507) 03-21 08: 59: 17.432: ОШИБКА / AndroidRuntime (5883): в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839) 03-21 08:59: 17.432: ОШИБКА / AndroidRuntime (5883): на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:597) 03-21 08: 59: 17.432: ERROR / AndroidRuntime (5883): на dalvik.system.NativeStart.main (собственный метод)

Спасибо. Удалось ее решить. Совместное использование кода в интересах других Пользовательский класс, который решил эту проблему. на основе ссылки @ Janardhanan.S.

public class BitmapResizer {

public static Bitmap decodeImage(Resources res, int id ,int requiredSize){
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, id, o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=requiredSize;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeResource(res, id, o2);
    } catch (Exception e) {

    }
    return null;
}

}

//Class call
int requiredsize = 100; // Still playing around with this number to find the optimum value
img_topLeft.setImageBitmap(BitmapResizer.decodeImage(getResources(),
        AlphResourceSet.R.drawable.xxx, requiredsize));
11
задан John Giotta 12 December 2011 в 18:59
поделиться