Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFile.

Prototype

public static Bitmap decodeFile(String pathName, Options opts) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:eu.liveandgov.ar.utilities.OS_Utils.java

/**
 * Load Bitmap for Entity found by 'id' stored locally in file system in Models3D.
 *  //from   w ww  .  j ava  2s .c  o  m
 * @param id : of the Entity
 * @return Bitmap of jpg image of the 3d model.
 */
public static Bitmap loadImagefromSD(String folderModels3DSTR, int id) {

    String Folder_STR = folderModels3DSTR + "/" + id + "/";

    String fileSCRSHOT_STR = OS_Utils.findfileAndroid("AR_" + id + "_1.jpg", Folder_STR);

    if (fileSCRSHOT_STR.length() > 0) {
        // Resize and load
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 6;  // Resize
        Log.e("DECODING", Folder_STR + "/" + fileSCRSHOT_STR);
        Bitmap bm = BitmapFactory.decodeFile(Folder_STR + "/" + fileSCRSHOT_STR, options);
        //bmImg  = Bitmap.createScaledBitmap(bmImg, 40, 40, false);
        return bm;
    } else {
        Log.e("loadImagefromSD", "Nothing at:" + Folder_STR + "/" + "AR_" + id + "_1.jpg");
        return null;
    }
}

From source file:com.truebanana.bitmap.BitmapUtils.java

public static BitmapFactory.Options getBounds(File file, BitmapFactory.Options options) {
    options.inJustDecodeBounds = true;// ww w  . ja va2s  . c om
    BitmapFactory.decodeFile(file.getPath(), options);
    return options;
}

From source file:Main.java

public static Bitmap loadFile(String path, int size) {
    Options options = new Options();
    options.inJustDecodeBounds = true;//from   w  w  w  .  j a v a  2  s .  co  m
    BitmapFactory.decodeFile(path, options);
    int w = options.outWidth;
    int h = options.outHeight;
    int sc = 1;
    if (w > size || h > size)
        if (w > h) {
            sc = Math.round((float) w / (float) size);
        } else {
            sc = Math.round((float) h / (float) size);
        }
    options.inJustDecodeBounds = false;
    options.inSampleSize = sc;
    try {
        return BitmapFactory.decodeFile(path, options);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static String getImageOrientation(String filepath) {
    if (filepath == null) {
        Log.e(TAG, "GPrintCommon : getImageOrientation() : filepath is null!");
        return unknown;
    }//from  w  w w  .ja  v  a 2 s . c  o m

    int imageWidth, imageHeight;

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (exif != null && exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0) > 0) {
        imageWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1);
        imageHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1);
    } else {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        Bitmap image = BitmapFactory.decodeFile(filepath, options);
        if (image == null) {
            Log.e(TAG, "GPrintCommon : getImageOrientation() : image is invalid. " + filepath);
            return unknown;
        }

        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
    }

    if (imageWidth > imageHeight) {
        return landscape;
    }

    return portrait;
}

From source file:Main.java

public static Bitmap decodeFile(File file, int size, boolean square) {
    try {//from w w w. j av  a  2s. co m
        //decode image size
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inJustDecodeBounds = true;
        Bitmap bitmapSize = BitmapFactory.decodeStream(new FileInputStream(file), null, bitmapOptions);

        //Find the correct scale value. It should be the power of 2.
        if (size > 0) {
            int width_tmp = bitmapOptions.outWidth, height_tmp = bitmapOptions.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < size || height_tmp / 2 < size)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }
            //decode with inSampleSize
            bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = scale;
            bitmapOptions.inScaled = true;
            bitmapSize = null;
            if (square) {
                return cropToSquare(BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions));
            }
            return BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
        }
        return null;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {//from  ww w .  j  av a2s . c o m
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}

From source file:com.google.android.panoramio.BitmapUtilsTask.java

/**
 * Loads a bitmap from the specified url.
 * //  w  w w .  ja  v  a 2 s  . co  m
 * @param url The location of the bitmap asset
 * @return The bitmap, or null if it could not be loaded
 * @throws IOException
 * @throws MalformedURLException
 */
public Bitmap getBitmap(final String string, Object fileObj) throws MalformedURLException, IOException {
    File file = (File) fileObj;
    // Get the source image's dimensions
    int desiredWidth = 1000;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    if (!file.isFile()) {
        InputStream is = (InputStream) new URL(string).getContent();
        BitmapFactory.decodeStream(is, null, options);
        is.close();

    } else {
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    }
    int srcWidth = options.outWidth;
    int srcHeight = options.outHeight;

    // Only scale if the source is big enough. This code is just trying
    // to fit a image into a certain width.
    if (desiredWidth > srcWidth)
        desiredWidth = srcWidth;

    // Calculate the correct inSampleSize/scale value. This helps reduce
    // memory use. It should be a power of 2
    int inSampleSize = 1;
    while (srcWidth / 2 > desiredWidth) {
        srcWidth /= 2;
        srcHeight /= 2;
        inSampleSize *= 2;
    }
    // Decode with inSampleSize
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = inSampleSize;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPurgeable = true;
    Bitmap sampledSrcBitmap;
    if (!file.isFile()) {
        InputStream is = (InputStream) new URL(string).getContent();
        sampledSrcBitmap = BitmapFactory.decodeStream(is, null, options);
        is.close();
    } else {
        sampledSrcBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    }
    return sampledSrcBitmap;

}

From source file:com.lightbox.android.bitmap.BitmapUtils.java

/**
 * Read a bitmap from disk//from ww  w  . j a  v  a  2s .  c om
 * @param absoluteFileName
 * @param config optional Bitmap.Config. If null, the default ARGB_8888 will be used.
 * @return
 */
public static Bitmap readBitmapFromFile(String absoluteFileName, Bitmap.Config config, int sampleSize) {
    Options opts = new Options();
    opts.inPreferredConfig = config == null ? Config.ARGB_8888 : config;
    opts.inDither = true;
    opts.inSampleSize = sampleSize;
    try {
        return BitmapFactory.decodeFile(absoluteFileName, opts);
    } catch (OutOfMemoryError e1) {
        try {
            opts.inPreferredConfig = Config.RGB_565;
            return BitmapFactory.decodeFile(absoluteFileName, opts);
        } catch (OutOfMemoryError e2) {
            throw e2;
        }
    }
}

From source file:com.exzogeni.dk.graphics.Bitmaps.java

@Nullable
private static Bitmap decodeFileInternal(String filePath, int hwSize) {
    if (hwSize > 0) {
        final BitmapFactory.Options ops = new BitmapFactory.Options();
        ops.inJustDecodeBounds = true;/*from  w w w  .  j  a va  2s  .  co m*/
        BitmapFactory.decodeFile(filePath, ops);
        ops.inSampleSize = calculateInSampleSize(ops, hwSize);
        ops.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, ops);
    }
    return BitmapFactory.decodeFile(filePath);
}