コード例 #1
0
ファイル: TaskFragment.java プロジェクト: Gris87/EGE
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle aArgs = getArguments();

    if (aArgs != null) {
      mTask = GlobalData.tasks.get(aArgs.getInt(GlobalData.TASK_ID));
    } else {
      Log.w(TAG, "There is no arguments");
      mTask = GlobalData.tasks.get(0);
    }
  }
コード例 #2
0
ファイル: TaskFragment.java プロジェクト: Gris87/EGE
    private String getImage() throws IOException {
      String aFileName =
          GlobalData.selectedLesson.getId() + "/" + String.valueOf(mTask.getId() + 1) + ".png";

      if (new File(GlobalData.PATH_ON_SD_CARD + aFileName).exists()) {
        Drawable aDrawable = Drawable.createFromPath(GlobalData.PATH_ON_SD_CARD + aFileName);

        if (aDrawable != null) {
          return "file://" + GlobalData.PATH_ON_SD_CARD + aFileName;
        } else {
          Log.i(TAG, "Invalid file on sdcard: " + GlobalData.PATH_ON_SD_CARD + aFileName);
        }
      }

      if (Utils.checkWifiOrNet(getCalculateActivity())) {
        // Download file
        URL aUrl = new URL(GlobalData.PATH_ON_NET + aFileName);

        HttpURLConnection aConnection = (HttpURLConnection) aUrl.openConnection();
        aConnection.setReadTimeout(300000);
        aConnection.setConnectTimeout(300000);
        aConnection.setRequestMethod("GET");
        aConnection.setDoInput(true);

        aConnection.connect();
        InputStream in = aConnection.getInputStream();

        boolean aFromInternet = true;

        try {
          byte[] aBuffer = new byte[4096];

          new File(GlobalData.PATH_ON_SD_CARD + GlobalData.selectedLesson.getId()).mkdirs();
          new File(GlobalData.PATH_ON_SD_CARD + ".nomedia").createNewFile();

          FileOutputStream aNewFile = new FileOutputStream(GlobalData.PATH_ON_SD_CARD + aFileName);

          do {
            int aBytes = in.read(aBuffer);

            if (aBytes <= 0) {
              break;
            }

            aNewFile.write(aBuffer, 0, aBytes);
          } while (true);

          aNewFile.close();

          aFromInternet = false;
        } catch (Exception e) {
          Log.w(TAG, "Problem while saving image on sd card", e);
        }

        try {
          in.close();
        } catch (Exception e) {
          Log.w(TAG, "Problem while saving image on sd card", e);
        }

        if (aFromInternet) {
          return GlobalData.PATH_ON_NET + aFileName;
        } else {
          Drawable aDrawable = Drawable.createFromPath(GlobalData.PATH_ON_SD_CARD + aFileName);

          if (aDrawable != null) {
            return "file://" + GlobalData.PATH_ON_SD_CARD + aFileName;
          } else {
            Log.w(
                TAG,
                "Invalid file on SD card after downloading: "
                    + GlobalData.PATH_ON_SD_CARD
                    + aFileName);
          }
        }
      }

      return null;
    }