예제 #1
0
  /**
   * Create a gold price object from a correctly formatted csv file
   *
   * @param csvFile The csv file downloaded from yahoo
   * @throws IOException
   */
  private void parseCsvFile(String csvFile) throws IOException {

    // read file
    byte[] data = com.tools.Tools.readFile(csvFile);
    if (data == null) throw new IOException("File could not be read");
    String string = new String(data, CHARSET);

    // split by commas
    ArrayList<String> items = new ArrayList<String>(Arrays.asList(string.split("\\s*,\\s*")));

    // make sure has 4 items
    if (items.size() != N_FIELDS)
      throw new IOException("File is not formatted correctly. Expect 4 fields");

    // check ticker
    if (!items.get(0).equals(getFullTicker()))
      throw new IOException("Ticker symbol is not correct. Received " + items.get(0));

    // strip off end of line
    items.set(3, items.get(3).replaceAll("\\r\\n", ""));

    // create date
    SimpleDateFormat format = new SimpleDateFormat("\"MM/dd/yyyy\"\"hh:mma\"", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
    try {
      date = format.parse(items.get(2) + items.get(3));
    } catch (ParseException e) {
      throw new IOException("Bad file format: " + items.get(2) + items.get(3));
    }

    // grab price
    price = Double.parseDouble(items.get(1));
  }
예제 #2
0
  /**
   * Determine which file to write to. It will be a random file on the external directory
   *
   * @return The file
   * @throws IOException
   */
  private String getFileToWriteTo() throws IOException {
    // determine where to save file
    String path = Utils.getExternalStorageTopPath();
    if (path == null) throw new IOException("Cannot write to external storage");

    // create a random file to write to
    path = path + com.tools.Tools.randomString(20) + ".tmp";

    return path;
  }
예제 #3
0
  /**
   * Try to create the thumbnail from the full picture
   *
   * @param thumbPath the desired thumbnail path
   * @param fullFile the path to the full file
   * @param maxPixelSize the maximum sixe in pixels for any dimension of the thumbnail.
   * @param forceBase2 forcing the downsizing to be powers of 2 (ie 2,4,8). Faster, but obviously
   *     less specific size is allowable.
   * @return true if successful, false otherwise
   */
  public static boolean createThumbnailFromFull(
      String thumbPath, String fullFile, int maxPixelSize, boolean forceBase2) {

    // open the full file
    if (fullFile == null || thumbPath == null || fullFile.length() == 0 || thumbPath.length() == 0)
      return false;
    RandomAccessFile f = null;
    try {
      f = new RandomAccessFile(fullFile, "r");
    } catch (FileNotFoundException e) {
      return false;
    }

    // read the file data
    byte[] b = null;
    ExifInterface exif = null;
    try {
      b = new byte[(int) f.length()];
      f.read(b);
      f.close();

      // read the orientaion
      exif = new ExifInterface(fullFile);
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }

    // grab the rotation
    int rotation =
        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    // create the byte array
    byte[] thumbnail = com.tools.Tools.makeThumbnail(b, rotation, maxPixelSize, forceBase2);

    // save the thumbnail
    SuccessReason thumbnailSave =
        com.tools.Tools.saveByteDataToFile(
            null, thumbnail, "", false, thumbPath, ExifInterface.ORIENTATION_NORMAL, false);

    return thumbnailSave.getSuccess();
  }
예제 #4
0
  /**
   * Read a picture from the given path, return null if unsuffessful <br>
   * Make sure to NOT call on main UI thread because it's slow <br>
   * Will be properly rotated based on exif data stored in image
   *
   * @param path
   * @return the bitmap
   */
  public static Bitmap getThumbnail(String path) {
    // open the path if it exists
    if (path != null && path.length() != 0 && (new File(path)).exists()) {

      // read the bitmap
      Bitmap bmp = BitmapFactory.decodeFile(path);
      if (bmp == null) return bmp;

      // now do the rotation
      float angle = com.tools.Tools.getExifOrientationAngle(path);
      if (angle != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
      }
      return bmp;
    } else return null;
  }
예제 #5
0
  /**
   * Return the properly rotated full image, null if can't be found or any other error <br>
   * Make sure to only call NOT on main ui thread <br>
   * It will be scaled down, so as not to cause memory crash
   *
   * @param path the path to the file
   * @param desiredWidth the desired width of the image, will not necessarily create a bitmap of
   *     this exact size, but no larger than this
   * @param desiredHeight the desired height of the image, will not necessarily create a bitmap of
   *     this exact size, but no larger than this
   * @return The bitmap or null if failed.
   */
  public static Bitmap getFullImage(String path, int desiredWidth, int desiredHeight) {
    try {
      if (path != null && path.length() != 0 && (new File(path)).exists()) {

        // make the file
        File file = new File(path);

        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, o);

        // find the correct scale size
        double scale =
            ((double)
                Math.max((double) o.outHeight / desiredHeight, (double) o.outWidth / desiredWidth));
        int intScale = (int) Math.pow(2, Math.ceil(com.tools.MathTools.log2(scale)));

        // now actually do the resizeing
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = intScale;
        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
        float angle = com.tools.Tools.getExifOrientationAngle(path);

        // now do the rotation
        if (angle != 0) {
          Matrix matrix = new Matrix();
          matrix.postRotate(angle);

          bitmap =
              Bitmap.createBitmap(
                  bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }

        return bitmap;
      } else return null;
    } catch (FileNotFoundException e) {
      return null;
    } catch (Exception e) {
      return null;
    }
  }