2016-08-23 10 views
12

'da belirli bir süre için güncelledikten sonra bildirimin güncellenmesi durdu. Bu nedenle, uygulamamda bir bildirim var. Bildirimin bazı metni ve başlangıçta doldurduğum bir LruCache'den aldığım bir görüntü var. Sorun, benim durumumda Kit-Kat aygıtlarında (Moto E, XIomi) durumunda, önceden belirlenmiş bir sayıyı güncellediyse, bildirimin güncelleştirilmesinin durmasıdır, n (bu n genellikle her aygıt için sabittir) < 10). BuAndroid: Kit-Kat

  String title = CommonUtils.getTitleFromID(id, context); 
      Bitmap bitmap = DataProvider.getInstance(context).diskLruImageCache.getBitmap(id + ""); 
      if (playbackPaused) { 
       bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti); 
       smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti); 
       remoteViews.setImageViewResource(R.id.pause2, R.drawable.pause_noti); 
       playbackPaused = false; 
       Log.d(TAG, "noti-false"); 
      } 

      bigView.setTextViewText(R.id.title, title + ""); 
      bigView.setImageViewBitmap(R.id.img, bitmap); 

      smallView.setTextViewText(R.id.title1, title); 
      smallView.setImageViewBitmap(R.id.img1, bitmap); 

      mNotificationManager.notify(NOTIFY_ID, notification); 

güncellemek için benim kod ben hata ayıklama biraz ve tüm çalıştı ve ben, bitmap ayarlamak (ve sadece
bitmap = null do) sonra her şey gayet iyi çalışıyor yoksa ben bulundu olduğunu.

Bunun neden olduğunu anlamıyorum. Bu sorun, Nexus 5 (Lollipop) ve diğer Android 5 telefonlarında test ettiğimden beri Android sürümüne özgü ancak bu olmadı. Bunun arkasındaki nedenin ne olabileceği hakkında bir fikri var mı?

Tam bir durum beklemiyorum. Doğru yönde bazı fikirler bile çok yardımcı olacaktır. Teşekkürler !! ihtiyaç duyulabilecek durumda ben de benim DiskLruCache kodunu ekliyorum

,

public class DiskLruImageCache { 

    private DiskLruCache mDiskCache; 
    private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG; 
    private int mCompressQuality = 70; 
    private static final int APP_VERSION = 1; 
    private static final int VALUE_COUNT = 1; 
    private static final String TAG = "DiskLruImageCache"; 

    public DiskLruImageCache(Context context,String uniqueName, int diskCacheSize, 
           Bitmap.CompressFormat compressFormat, int quality) { 
     try { 
      final File diskCacheDir = getDiskCacheDir(context, uniqueName); 
      mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize); 
      mCompressFormat = compressFormat; 
      mCompressQuality = quality; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor) 
      throws IOException { 
     OutputStream out = null; 
     try { 
      out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE); 
      return bitmap.compress(mCompressFormat, mCompressQuality, out); 
     } finally { 
      if (out != null) { 
       out.close(); 
      } 
     } 
    } 


    private boolean writeUriToFile(Uri bitmap, DiskLruCache.Editor editor) 
      throws IOException, FileNotFoundException { 
     OutputStream out = null; 
     try { 
      out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE); 
      return true; 
     } finally { 
      if (out != null) { 
       out.close(); 
      } 
     } 
    } 

    private File getDiskCacheDir(Context context, String uniqueName) { 

     // Check if media is mounted or storage is built-in, if so, try and use external cache dir 
     // otherwise use internal cache dir 
     final String cachePath = 
       Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || 
         !Utils.isExternalStorageRemovable() ? 
         Utils.getExternalCacheDir(context).getPath() : 
         context.getCacheDir().getPath(); 

     return new File(cachePath + File.separator + uniqueName); 
    } 

    public void put(String key, Bitmap data) { 

     DiskLruCache.Editor editor = null; 
     try { 
      editor = mDiskCache.edit(key); 
      if (editor == null) { 
       return; 
      } 

      if(writeBitmapToFile(data, editor)) { 
       mDiskCache.flush(); 
       editor.commit(); 
       if (BuildConfig.DEBUG) { 
        Log.d("cache_test_DISK_", "image put on disk cache " + key); 
       } 
      } else { 
       editor.abort(); 
       if (BuildConfig.DEBUG) { 
        Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key); 
       } 
      } 
     } catch (IOException e) { 
      if (BuildConfig.DEBUG) { 
       Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key); 
      } 
      try { 
       if (editor != null) { 
        editor.abort(); 
       } 
      } catch (IOException ignored) { 
      } 
     } 

    } 

    public void put(String key, Uri data) { 

     DiskLruCache.Editor editor = null; 
     try { 
      editor = mDiskCache.edit(key); 
      if (editor == null) { 
       return; 
      } 

      if(writeUriToFile(data, editor)) { 
       mDiskCache.flush(); 
       editor.commit(); 
       if (BuildConfig.DEBUG) { 
        Log.d("cache_test_DISK_", "image put on disk cache " + key); 
       } 
      } else { 
       editor.abort(); 
       if (BuildConfig.DEBUG) { 
        Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key); 
       } 
      } 
     } catch (IOException e) { 
      if (BuildConfig.DEBUG) { 
       Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key); 
      } 
      try { 
       if (editor != null) { 
        editor.abort(); 
       } 
      } catch (IOException ignored) { 
      } 
     } 

    } 

    public synchronized Bitmap getBitmap(String key) { 

     Bitmap bitmap = null; 
     DiskLruCache.Snapshot snapshot = null; 
     try { 

      snapshot = mDiskCache.get(key); 
      if (snapshot == null) { 
       return null; 
      } 
      final InputStream in = snapshot.getInputStream(0); 
      if (in != null) { 
       final BufferedInputStream buffIn = 
         new BufferedInputStream(in, Utils.IO_BUFFER_SIZE); 
       bitmap = BitmapFactory.decodeStream(buffIn); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (snapshot != null) { 
       snapshot.close(); 
      } 
     } 

     if (BuildConfig.DEBUG) { 
      Log.d("cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key); 
     } 

     return bitmap; 

    } 

    public boolean containsKey(String key) { 

     boolean contained = false; 
     DiskLruCache.Snapshot snapshot = null; 
     try { 
      snapshot = mDiskCache.get(key); 
      contained = snapshot != null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (snapshot != null) { 
       snapshot.close(); 
      } 
     } 

     return contained; 

    } 

    public boolean removeKey(String key) { 

     boolean removed=false; 
     DiskLruCache.Snapshot snapshot = null; 
     try { 
      removed = mDiskCache.remove(key); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (snapshot != null) { 
       snapshot.close(); 
      } 
     } 
     return removed; 
    } 

    public void clearCache() { 
     if (BuildConfig.DEBUG) { 
      Log.d("cache_test_DISK_", "disk cache CLEARED"); 
     } 
     try { 
      mDiskCache.delete(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public File getCacheFolder() { 
     return mDiskCache.getDirectory(); 
    } 

} 
+0

kodunuzdaki ve olası bir nedeni hakkında sadece bir gözlem 96 olduğunu bildirir. Problem hakkında herhangi bir iz var mı? – eduyayo

+0

@eduyayo Hey cevabınız için teşekkürler, herhangi bir belirgin iz fark etmedim. Ama bitmap'i nerede ve nasıl serbest bırakmalıyım, bu konu benim için gerçekten önemli, ama bunu çözemiyorum. Değişiklikleri önerebilir misin? Teşekkürler !! "Kullanım" dan sonra – varunkr

+0

'bitmap.recycle()' yi çağırmalısınız. Yeni bir tane kullanmadan önce önceki kullanılan ve geri dönüşüme bir referans tutmak için sizi sımsıkıyorum ... – eduyayo

cevap

0

koymak eğer kitkat ve alt sürümleri için koşul ... becoz bildirim sisteminde renkte kitkat versiyon değişikliklerinden sonra .. Sen bitmapleri kurtararak değil ve yüzey bellek yetersiz kilitleniyor edilebilir: my mipmap img siz * 96

NOTIFICATION_ID= Integer.parseInt(arr_msg[7].toString()); 
mBuilder.setSmallIcon(R.mipmap.alert_nodification_icon) 
     .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.alert_nodification_icon)) 
     .setContentTitle(notification_vehicle_no) 
     .setStyle(new NotificationCompat.BigTextStyle() 
       .bigText(notification_msg)) 
     .setContentText(notification_msg);} 
    mBuilder.setContentIntent(contentIntent); 
    Notification notification = mBuilder.build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    mNotificationManager.notify(NOTIFICATION_ID,notification);