public static Bitmap getScaleOptionImageByScaleOfWinWidth( Context context, File imgFile, float scaleOfWinWidth) { if (imgFile == null) return null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; if (photoW == 0) { return null; } int th = (int) (DeviceUtil.getDeviceHeight(context)); int tw = (int) (DeviceUtil.getDeviceWidth(context)); tw = (int) (tw * scaleOfWinWidth); if (tw == 0) { return null; } th = (int) (tw * ((double) photoH / photoW)); int scaleFactor = Math.min(photoW / tw, photoH / th); bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions); return bitmap; }
public static Bitmap getOptionBitmapByScaleOfWinWidth( Context context, Uri imgUri, float scaleOfWinWidth) throws FileNotFoundException { if (imgUri == null) return null; // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; InputStream is = null; try { is = context.getContentResolver().openInputStream(imgUri); BitmapFactory.decodeStream(is, null, bmOptions); } catch (Exception e) { } finally { IOUtils.closeQuietly(is); } int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; int tw = (int) (DeviceUtil.getDeviceWidth(context) * scaleOfWinWidth); if (tw == 0) { return null; } int th = (int) (tw * ((double) photoH / photoW)); int targetW = tw; int targetH = th; // Determine how much to scale down the image int scaleFactor = Math.min(photoW / targetW, photoH / targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; InputStream nis = null; Bitmap bitmap = null; try { nis = context.getContentResolver().openInputStream(imgUri); bitmap = BitmapFactory.decodeStream(nis, null, bmOptions); } catch (Exception e) { } finally { IOUtils.closeQuietly(nis); } return bitmap; }