Android Open Source - android-screenshot-gallery Image Utils






From Project

Back to project page android-screenshot-gallery.

License

The source code is released under:

Apache License

If you think the Android project android-screenshot-gallery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.demondevelopers.screenshotgallery;
/*w ww.  ja v  a 2s.c om*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;


public final class ImageUtils
{
  public static Bitmap getThumbSizedBitmap(Context context, String filePath)
  {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    if(options.outWidth <= 0 || options.outHeight <= 0){
      return null;
    }
    options.inSampleSize = calcSampleSize(
      options.outWidth, options.outHeight,
      displayMetrics.widthPixels / 4, displayMetrics.heightPixels / 4);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
  }
  
  public static Bitmap getDisplaySizedBitmap(Context context, String filePath, BitmapFactory.Options options)
  {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    options.inSampleSize = 1;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    if(options.outWidth <= 0 || options.outHeight <= 0){
      return null;
    }
    options.inSampleSize = calcSampleSize(
      options.outWidth, options.outHeight,
      displayMetrics.widthPixels, displayMetrics.heightPixels);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
  }
  
  public static int calcSampleSize(int imageWidth, int imageHeight, int desiredWidth, int desiredHeight)
  {
    int sampleSize = 1;
    while((imageWidth > desiredWidth) && (imageHeight > desiredHeight)){
      sampleSize  <<= 1;
      imageWidth  >>= 1;
      imageHeight >>= 1;
    }
    return sampleSize;
  }
  
  @SuppressLint("DefaultLocale")
  public static String humanReadableByteCount(long bytes, boolean si)
  {
    int unit = si ? 1000 : 1024;
    if (bytes < unit) return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
  }
}




Java Source Code List

com.demondevelopers.screenshotgallery.AboutFragment.java
com.demondevelopers.screenshotgallery.BaseActivity.java
com.demondevelopers.screenshotgallery.BaseFragment.java
com.demondevelopers.screenshotgallery.FitTopDecorOnlyLayout.java
com.demondevelopers.screenshotgallery.GalleryApp.java
com.demondevelopers.screenshotgallery.ImageUtils.java
com.demondevelopers.screenshotgallery.MainActivity.java
com.demondevelopers.screenshotgallery.OverlayDecorLayout.java
com.demondevelopers.screenshotgallery.PictureActivity.java
com.demondevelopers.screenshotgallery.PictureFragment.java
com.demondevelopers.screenshotgallery.PictureGridFragment.java
com.demondevelopers.screenshotgallery.PictureView.java
com.demondevelopers.screenshotgallery.ThumbView.java