decode File to Bitmap - Android android.graphics

Android examples for android.graphics:Bitmap Create

Description

decode File to Bitmap

Demo Code

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {

  public static Bitmap decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;/*w w w  .  j  av a 2 s  .c o  m*/
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
        break;
      width_tmp /= 2;
      height_tmp /= 2;
      scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
    return bitmap;
  }

}

Related Tutorials