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) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static Drawable getDrawableFromFile(Context context, File pngFile, int density) {
    Bitmap bmp = BitmapFactory.decodeFile(pngFile.getPath());
    if (bmp != null)
        bmp.setDensity(density);//www .  j  a v  a2  s. c o m

    return new BitmapDrawable(context.getResources(), bmp);
}

From source file:Main.java

protected static Uri scaleDown(Uri imageUri, int targetSize, Context context) {
    System.gc();/*w w  w. jav  a2 s.  com*/
    String imagePath = getImagePath(imageUri, context);
    Bitmap currentImage = BitmapFactory.decodeFile(imagePath);

    int targetWidth = targetSize;
    int targetHeight = targetSize;

    int width = currentImage.getWidth();
    int height = currentImage.getHeight();

    if (width < targetWidth || height < targetHeight) {
        currentImage.recycle();
        currentImage = null;
        System.gc();
        return Uri.parse(imageUri.toString());
    }

    height = (int) (height * (float) targetWidth / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(currentImage, targetWidth, height, false);

    if (currentImage != scaledBitmap) {
        currentImage.recycle();
        currentImage = null;
    }
    System.gc();
    File imageFile;
    try {
        imageFile = new File(context.getCacheDir(), "vumatch-upload-00.jpeg");
        FileOutputStream output;

        output = new FileOutputStream(imageFile);
        boolean result = scaledBitmap.compress(CompressFormat.JPEG, 90, output);
        if (result) {
            scaledBitmap.recycle();
            scaledBitmap = null;
            return Uri.fromFile(imageFile);
        } else {
            return null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static BitmapDrawable loadAppIcon(String iconPath, Context context) {
    try {//from   www  .ja  v  a 2 s  . co m
        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        if (iconPath != null && !"".equals(iconPath)) {
            bitmap = BitmapFactory.decodeFile(iconPath);
        }

        if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
            Log.e("RecommAppsUtils", iconPath + " is not exist");
            return null;
        }
        int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
        float scale = densityDpi / STANDARD_DENSITYDPI;
        newBitmap = zoomBitmap(bitmap, (int) (APP_ICON_WIDTH * scale), (int) (APP_ICON_HEIGHT * scale));
        // Log.d("RecommAppsUtils", "densityDpi value : " + densityDpi);
        return new BitmapDrawable(context.getResources(), newBitmap);
    } catch (OutOfMemoryError error) {
        error.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void setFBImage(final String fbid, final Context context, final ImageView v) {
    new AsyncTask<String, Void, Bitmap>() {

        @Override//  w w w.j a va  2s .com
        protected Bitmap doInBackground(String... strings) {
            File img = new File(context.getFilesDir() + "/profile.jpg");
            Bitmap bmp = null;
            if (img.exists()) {
                try {
                    bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            } else {
                try {
                    URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid)
                            + "/picture?type=large&redirect=true&width=400&height=400");
                    bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                    FileOutputStream fOut = new FileOutputStream(img);
                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
            return bmp;
        }

        @Override
        protected void onPostExecute(Bitmap img) {
            if (img != null) {
                v.setImageBitmap(img);
            }
        }
    }.execute();
}

From source file:Main.java

private static Bitmap getImage(String imagePath) {
    final int MAX_TRIALS = 3;
    int trial = 0;
    Bitmap bitmap = null;/*from  w w w . j a  va 2 s . c  o  m*/
    do {
        try {
            bitmap = BitmapFactory.decodeFile(imagePath);
        } catch (OutOfMemoryError e) {
            System.gc();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                throw new RuntimeException("OoM");
            }
        }
    } while (trial++ != MAX_TRIALS);
    if (trial == MAX_TRIALS || bitmap == null) {
        throw new RuntimeException("OoM");
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap originalImg(File file) {
    Bitmap resizeBmp = null;//from  w  ww  . j  a v  a2 s . c o  m
    try {
        resizeBmp = BitmapFactory.decodeFile(file.getPath());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resizeBmp;
}

From source file:Main.java

public static Bitmap getBitmap(String fileName) {
    Bitmap bitmap = getBitmapFromCache(fileName);
    if (bitmap != null) {
        return bitmap;
    }//from w w  w .j  a va 2 s .com

    // modify the file attribute
    String filePath = getStorageDirectory() + File.separator + fileName;
    File file = new File(filePath);
    long time = new Date().getTime();
    file.setLastModified(time);
    return BitmapFactory.decodeFile(filePath);
}

From source file:Main.java

public static Bitmap loadBitmap(String path) {
    File file = new File(path);
    if (file.exists()) {
        return BitmapFactory.decodeFile(path);
    }//from w  w w.  ja  v  a  2s.  co m
    return null;

}

From source file:Main.java

public static Bitmap createScaledBitmap(String path, float scale, boolean filtering) {
    Bitmap src = BitmapFactory.decodeFile(path);
    int width = (int) (src.getWidth() * scale + 0.5f);
    int height = (int) (src.getHeight() * scale + 0.5f);
    return Bitmap.createScaledBitmap(src, width, height, filtering);
}

From source file:Main.java

/**
 * @param path//  ww w  .  ja  v  a2  s .c  om
 * @return
 */
public static Bitmap getBitMapFromFile(String path) {
    File imgFile = new File(path);
    Bitmap myBitmap = null;
    if (imgFile.exists()) {

        myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    }

    return myBitmap;
}