Esempio n. 1
0
  public void getPhoto(ImageView img) {
    final TL.Object location = getPhotoLocation();
    img.setTag(location);

    if (location == null) {
      img.setImageResource(getDefPhoto());
      return;
    }

    String path = FileQuery.exists(location);
    if (path != null) {
      Bitmap bmp = BitmapCache.get(path);
      if (bmp != null) {
        img.setImageBitmap(bmp);
        return;
      }
    }

    final WeakReference<ImageView> ref = new WeakReference<ImageView>(img);

    try {
      new FileQuery(
          location,
          null,
          new FileQuery.OnResultListener() {
            @Override
            public void onResult(TL.Object result) {
              ImageView img = ref.get();
              if (img == null || img.getTag() != location) return;

              final Bitmap bmp =
                  result != null
                      ? BitmapCache.loadBitmap(
                          result.getString("fileName"),
                          Main.SIZE_THUMB_USER,
                          Main.SIZE_THUMB_USER,
                          true,
                          false,
                          true)
                      : BitmapFactory.decodeResource(Main.main.getResources(), getDefPhoto());

              try {
                img.post(
                    new Runnable() {
                      @Override
                      public void run() {
                        ImageView img = ref.get();
                        if (img == null || img.getTag() != location) return;
                        img.setImageBitmap(bmp);
                      }
                    });
              } catch (Exception e) {
                Common.logError("error loading bitmap");
                e.printStackTrace();
              }
            }

            @Override
            public void onProgress(float progress) {}
          });
    } catch (Exception e) {
      Common.logError("error query file");
      e.printStackTrace();
    }
  }