Guest User

Untitled

a guest
Feb 5th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1.     public SettableFuture<Bitmap> getImage(int id) {
  2.         SettableFuture<Bitmap> fut = null;
  3.  
  4.         //imagesCache переменная типа LruChache
  5.         //Проверяем есть ли в кеше изображение
  6.         //Если нет, то
  7.         if (imagesCache.get(id) == null) {
  8.             //Отправляем запрос на передачу изображения
  9.             SessionRegistry.getInstance().get().send(new GetIpPhotoByIdRequest(id));
  10.  
  11.             //добавляем пустое будущее
  12.             imagesCache.put(id, new SettableFuture<Bitmap>());
  13.         } else {
  14.             //Иначе возвращаем изображение из кеша
  15.             fut = imagesCache.get(id);
  16.         }
  17.  
  18.         //Возвращаем будущее на битмап.
  19.         //Битмап появится когда придёт пакет с ним,
  20.         //либо не появится если произойдёт таймаут
  21.         return fut;
  22.     }
  23.  
  24.     public void addImage(final int id, final byte[] image) {
  25.         //Запихиваем изображение в кеш
  26.         SettableFuture<Bitmap> future = imagesCache.get(id);
  27.         Bitmap bitmap = null;
  28.  
  29.         if (image != null) {
  30.             try {
  31.                 bitmap = Glide.with(context)
  32.                         .load(image)
  33.                         .asBitmap()
  34.                         .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
  35.                         .get();
  36.  
  37.                 if (bitmap != null) {
  38.                     future.set(bitmap);
  39.                 } else {
  40.                     future.set(defaultImage);
  41.                 }
  42.             } catch (InterruptedException | ExecutionException e) {
  43.                 FileLog.getInstance().e(ImageReceiver.class.getName(), e);
  44.                 future.set(defaultImage);
  45.             }
  46.         }
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment