Пример #1
0
    private PrepareResult createBitmap(MediaGeoTask geoTask) throws IOException {
      Bitmap res = fetchMapPreview();
      Optimizer.BitmapInfo info = tryToLoadFromCache(geoTask.getStorageKey(), res);
      if (info != null) {
        return new PrepareResult(info.getWidth(), info.getHeight(), res);
      }

      int scale = 1;
      float density = application.getResources().getDisplayMetrics().density;
      if (density >= 1.5f) {
        scale = 2;
      }

      String url =
          "https://maps.googleapis.com/maps/api/staticmap?"
              + "center="
              + geoTask.getLatitude()
              + ","
              + geoTask.getLongitude()
              + "&zoom=15"
              + "&size="
              + (MAP_W / scale)
              + "x"
              + (MAP_H / scale)
              + "&scale="
              + scale
              + "&sensor=false"
              + "&format=jpg";

      HttpGet get = new HttpGet(url.replace(" ", "%20"));
      HttpResponse response = client.execute(get);
      if (response.getEntity().getContentLength() == 0) {
        throw new IOException();
      }

      if (response.getStatusLine().getStatusCode() == 404) {
        throw new IOException();
      }

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      response.getEntity().writeTo(outputStream);
      byte[] data = outputStream.toByteArray();

      Optimizer.BitmapInfo info1 = Optimizer.loadTo(data, res);

      putToDiskCache(geoTask.getStorageKey(), res, MAP_W, MAP_H);

      return new PrepareResult(info1.getWidth(), info1.getHeight(), res);
    }
Пример #2
0
    @Override
    protected boolean processTask(BaseTask task) throws Exception {
      if (!(task instanceof MediaGeoTask)) {
        return true;
      }

      MediaGeoTask geoTask = (MediaGeoTask) task;
      PrepareResult result = createBitmap(geoTask);
      Bitmap mapPreview = fetchMapPreview();
      int[] sizes =
          drawPreview(
              result.getBitmap(), result.getW(), result.getH(), mapPreview, geoTask.isOut());
      imageCache.putFree(result.getBitmap(), SIZE_MAP_PREVIEW);
      onImageLoaded(mapPreview, sizes[0], sizes[1], task, SIZE_MAP_PREVIEW);
      return true;
    }