/** * 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; }
/** * 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; } }