2016-04-11 29 views
0

Proje: https://github.com/MediaPortal1/HowToDraw/blob/master/app/src/main/java/com/poltavets/app/setImageBitmap Hatası, ImageView çağrı OutOfMemory GitHub üzerinde hata ile

Ben metod Projemdeki img.setImageResources (R.id.image), ama çoğu zaman bu yöntemi kullandığınızda o OutOfMemory çağrı offten veya değiştirme ekran yönlendirmesi. Bunu koymak

Handler handler=new Handler(){ 
      @Override 
      public void handleMessage(Message msg) { 
       if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 
        imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,100,100)); 
       else imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,400,400)); 
      } 
     }; 
     handler.sendEmptyMessage(image); 

:

 imageView.setImageResource(image); 

İÇİN: 2 Geçen bu makalenin yöntemleri ve benim kodunu değiştirmek https://developer.android.com/training/displaying-bitmaps/load-bitmap.html Kopya yapıştır: Ben "doğru" yükleme bitmap görüntüleri ile ilgili bu madde bulundu Handler için yöntemler ve daha hızlı çalışıyordu. Ve yönlendirme şartlarını ekliyorum, çünkü ImageView daha az arazi kullanımı ve bitmap daha küçük bir çözünürlüğe sahip olmalıdır. İyi çalıştı, everithink iyiydi. Ama bu projede başka bir detayla çalıştım (sadece projeme resim ekledim) ve bu projeyi 2-3 kez yeniden inşa ediyorum ve aktivitemi başlattığımda OutOfMemory hatası tekrar ortaya çıkıyor. Bu hatanın ortaya çıkmasından önce her zaman değil, ama şimdi Faaliyete başladığımda her zaman DAİLE! Nasıl? Bu aktivitenin kodunu karıştırmıyorum? Resimlerim gerçekten düşük momor. Görüntülerin maksimum boyutu 200kb. 100kb boyutunda görüntü OutOfMemory'yi nasıl arayabilir?

Hata: ChangeImage Kodu (Hata buradadır)

Process: com.poltavets.app.howtodraw, PID: 28720 java.lang.OutOfMemoryError: Failed to allocate a 35389452 byte allocation with 5482128 free bytes and 5MB until OOM 
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:655) 
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488) 
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:511) 
    at com.poltavets.app.howtodraw.view.HowTo.decodeSampledBitmapFromResource(HowTo.java:224) 
    at com.poltavets.app.howtodraw.view.HowTo$3.handleMessage(HowTo.java:185) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5546) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 

: google makalesinden

public void changeImageSrc(int image,int count,int position,String name) { 
      filename=name+"_"+image+"_"+position; 
      imagenumber=position; 
      if(move!=null || back!=null) { 
       move.setEnabled(false); 
       back.setEnabled(false); 
      } 
      Handler handler=new Handler(){ 
       @Override 
       public void handleMessage(Message msg) { 
        if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 
         imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,100,100)); 

else imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,400,400)); // OUT OF MEMORY ERROR 
       } 
      }; 
      handler.sendEmptyMessage(image); 

      getSupportActionBar().setTitle(getResources().getString(R.string.title_how_to) + " " + name + ": " + (position+1)+"/"+(count)); 
       if(position==0 || position+1==count){ 
        if(position==0){ 
         findViewById(R.id.backBtn).setEnabled(false); 
         findViewById(R.id.backBtn).setVisibility(View.INVISIBLE); 
        } 
        else { 
         findViewById(R.id.moveBtn).setEnabled(false); 
         findViewById(R.id.moveBtn).setVisibility(View.INVISIBLE); 
         Toast.makeText(getApplicationContext(),getString(R.string.finish),Toast.LENGTH_SHORT).show(); 
        } 
       } 
       else { 
        showNavButtons(); 
       } 
      if(move!=null || back!=null) { 
       move.setEnabled(true); 
       back.setEnabled(true); 
      } 

2 yöntem:

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) { 

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

cevap

0

Sadece hiçbir şey yapmadı. Bu projeyi 3-4 kez yeniden inşa ettim, ama yardımcı olmadı. Ama şimdi hiçbir değişiklik yapmadan şimdi yeniden yapıyorum ve işe yaradı.

+1

Lütfen cevaplarınızda yemin etmeyin. – JAL

+0

Görünüşte, uygulamadaki resimlerle işlerken projeleri sık sık temizlemeniz/yeniden oluşturmanız gerekiyor gibi görünüyor. App 3 görüntüleri dahil (birlikte ~ 1.5mb vardı) ve uygulama çöktü. Ne yaptığımın bir önemi yok (50kb'ye küçültmeyi başardım). Projeyi temizledikten/yeniden inşa ettikten sonra aniden çalıştım (daha küçük resimlerle). Önbelleğe alınan görüntüler temiz/yeniden oluşturmadan önce kullanılmış ve bu nedenle görüntüleri nasıl optimize ettiğimin önemi yoktu. – vanomart