コード例 #1
0
  @Override
  public void onResponse(Call<Radar> call, Response<Radar> response) {
    Log.v(this, "onResponse");
    Radar radar = response.body();
    Log.v(this, "Response:" + radar);

    saveImages(radar);
  }
コード例 #2
0
  private void saveImages(Radar radar) {

    count = 0;
    final int imageCount = radar.getImages().size();

    File sd = Environment.getExternalStorageDirectory().getAbsoluteFile();
    final File dest = new File(sd, "radar/");

    if (!dest.exists()) {
      dest.mkdirs();
    }

    // delete any files older than 24h
    if (dest.listFiles() != null) {
      for (File f : dest.listFiles()) {
        long diff = (new Date().getTime() - new Date(f.lastModified()).getTime()) / 60 / 60 / 24;
        if (diff > 24) {
          f.delete();
        }
      }
    }

    for (final Image image : radar.getImages()) {
      // Keep a count to compare to imageCount so we know when we're finished
      count++;

      // Check if the file exists. We only download it if it doesn't
      File currentImage = new File(dest, image.getSrc());
      if (currentImage.exists()) {
        Log.v(this, "NOT Downloading image. Already exists locally:" + image.getSrc());

        if (count == imageCount) {
          Log.d(this, "No new images to download");
          stopSelfWithSuccess();
        }
      } else {
        Log.v(
            this, "Downloading image:" + "http://www.met.ie/weathermaps/radar2/" + image.getSrc());

        OkHttpClient client = new OkHttpClient();

        Request request =
            new Request.Builder()
                .url("http://www.met.ie/weathermaps/radar2/" + image.getSrc())
                .addHeader("Referer", "http://www.met.ie/latest/rainfall_radar.asp")
                .build();

        client
            .newCall(request)
            .enqueue(
                new okhttp3.Callback() {
                  @Override
                  public void onFailure(okhttp3.Call call, IOException e) {
                    Log.e(this, "onFailure" + e.getMessage(), e);

                    stopSelfWithFailure();
                  }

                  @Override
                  public void onResponse(okhttp3.Call call, okhttp3.Response response)
                      throws IOException {

                    Log.v(this, "Image onResponse");

                    String filename = image.getSrc();

                    File file = new File(dest, filename);

                    try {
                      Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                      FileOutputStream out = new FileOutputStream(file);
                      bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                      out.flush();
                      out.close();

                    } catch (Exception e) {
                      e.printStackTrace();
                    }

                    Log.v(this, "Count:" + count + " Imagecount:" + imageCount);
                    if (count == imageCount) {
                      Log.d(this, "Updating widgets. Src:" + image.getSrc());

                      stopSelfWithSuccess();
                    }
                  }
                });
      }
    }
  }