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:com.oakesville.mythling.app.AppData.java

public Bitmap readImageBitmap(String path) {
    File cacheDir = appContext.getCacheDir();
    File imageFile = new File(cacheDir.getPath() + "/" + path);
    if (imageFile.exists()) {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "reading image from file: " + imageFile);
        // avoid out-of-memory errors by checking image dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imageFile.getPath(), options);
        // calculate sample size (based on screen size)
        options.inSampleSize = calculateImageSampleSize(options.outWidth, options.outHeight, getScreenSize().x,
                getScreenSize().y);//  w  ww. j  av  a 2 s .  co  m
        options.inJustDecodeBounds = false;
        // TODO: save resampled image to save disk space
        return BitmapFactory.decodeFile(imageFile.getPath(), options);
    } else {
        return null;
    }
}

From source file:ch.ethz.dcg.jukefox.commons.utils.AndroidUtils.java

public static Bitmap getBitmapFromFile(String path, int maxSize) {
    Bitmap bitmap = null;// ww w .j a va  2s. c o m
    int dummySize = maxSize * maxSize / 4; // KB
    while (bitmap == null && dummySize <= maxSize * maxSize * 4) {
        try {
            int sampleFactor = getSampleFactor(path, maxSize);
            BitmapFactory.Options resample = new BitmapFactory.Options();
            resample.inSampleSize = sampleFactor;
            bitmap = BitmapFactory.decodeFile(path, resample);
        } catch (Throwable e) {
            // Avoid that heap has to be grown for the BitmapFactory,
            // as this would lead to an out of memory error
            int[] dummyArray = new int[dummySize * 1024];
            // Avoid being eliminated by optimization of compiler
            if (dummyArray != null) {
                dummyArray = null;
                System.gc();
            }
            Log.w(TAG, e);
            dummySize *= 2;
        }
    }
    return bitmap;

}

From source file:es.uma.lcc.tasks.EncryptionUploaderTask.java

private void displayEncryptedImage(String path) {
    ImageView imageView = (ImageView) mMainActivity.findViewById(R.id.imageView);
    imageView.setVisibility(ImageView.VISIBLE);

    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    int w = options.outWidth;
    int h = options.outHeight;

    if (w >= 2048 || h >= 2048) {
        double factor = (w > h ? 2048.0 / w : 2048.0 / h);
        imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, (int) (factor * w), (int) (factor * h), false));
    } else {/*from   www .  j  a  v a 2s.c  o m*/
        imageView.setImageBitmap(bmp);
    }
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

/**
 * Compress and resize the Image/*from w  w  w.j a  v a2s  .c  om*/
 * 
 * <br />
 * , ,  
 * 
 * @param targetFile
 * @param quality
 *            , 0~100, recommend 100
 * @return
 * @throws IOException
 */
public File compressImage(File targetFile, int quality) throws IOException {
    String filepath = targetFile.getAbsolutePath();

    // 1. Calculate scale
    int scale = 1;
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filepath, o);
    if (o.outWidth > IMAGE_MAX_WIDTH || o.outHeight > IMAGE_MAX_HEIGHT) {
        scale = (int) Math.pow(2.0, (int) Math
                .round(Math.log(IMAGE_MAX_WIDTH / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        // scale = 2;
    }
    Log.d(TAG, scale + " scale");

    // 2. File -> Bitmap (Returning a smaller image)
    o.inJustDecodeBounds = false;
    o.inSampleSize = scale;
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, o);

    // 2.1. Resize Bitmap
    // bitmap = resizeBitmap(bitmap, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);

    // 3. Bitmap -> File
    writeFile(targetFile.getName(), bitmap, quality);
    Log.i(TAG, "TargetFile's name is " + targetFile.getName());
    // 4. Get resized Image File
    //      String filePath = getMd5(targetFile.getAbsolutePath());
    String filePath = targetFile.getName();
    Log.i(TAG, targetFile.getAbsolutePath());
    File compressedImage = mContext.getFileStreamPath(filePath);
    return compressedImage;
}

From source file:com.life.wuhan.util.ImageDownloader.java

public static Bitmap createFromCache(String file) {
    try {//from w  w w  .j  av  a2  s . com
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inDither = true;
        return BitmapFactory.decodeFile(file, options);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.androidquery.simplefeed.activity.PostActivity.java

private Options getSize(File file) {

    BitmapFactory.Options options = null;

    options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w  ww  . j ava2 s.  c  om*/

    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    return options;
}

From source file:com.abc.driver.PersonalActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CellSiteConstants.TAKE_USER_PORTRAIT
            || requestCode == CellSiteConstants.PICK_USER_PORTRAIT) {
        Uri uri = null;//from w  w w  .  java  2s  .c om
        if (requestCode == CellSiteConstants.TAKE_USER_PORTRAIT) {
            uri = imageUri;

        } else if (requestCode == CellSiteConstants.PICK_USER_PORTRAIT) {
            uri = data.getData();

        }

        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            imageUri = Uri.fromFile(new File(filePath));
        } else // This is a bug, in some cases, some images like
        {
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                Toast.makeText(this, R.string.sdcard_occupied, Toast.LENGTH_SHORT).show();
                return;
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File tmpFile = new File(app.regUserPath + File.separator + "IMG_" + timeStamp + ".png");
            File srcFile = new File(uri.getPath());
            if (srcFile.exists()) {
                try {
                    Utils.copyFile(srcFile, tmpFile);
                    app.getUser().setProfileImageUrl(tmpFile.getAbsolutePath());
                } catch (Exception e) {
                    Toast.makeText(this, R.string.create_tmp_file_fail, Toast.LENGTH_SHORT).show();
                    return;
                }
            } else {
                Log.d(TAG, "Logic error, should not come to here");
                Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
                return;
            }

            imageUri = Uri.fromFile(tmpFile);
        }

        doCrop();
        Log.d(TAG, "onActivityResult PICK_PICTURE");

    } else if (requestCode == CellSiteConstants.CROP_PICTURE) {
        Log.d(TAG, "crop picture");
        // processFile();

        if (data != null) {
            Bundle extras = data.getExtras();
            Bitmap photo = extras.getParcelable("data");

            app.setPortaritBitmap(photo);
            mUserPortraitIv.setImageBitmap(photo);
            mUpdateImageTask = new UpdateImageTask();
            mUpdateImageTask.execute("" + app.getUser().getId(), Utils.bitmap2String(photo),
                    CellSiteConstants.UPDATE_USER_PORTRAIT_URL);

            isPortraitChanged = true;
        }
    } else if (requestCode == CellSiteConstants.TAKE_IDENTITY
            || requestCode == CellSiteConstants.PICK_IDENTITY) {

        Uri uri = null;
        if (requestCode == CellSiteConstants.TAKE_IDENTITY) {
            uri = imageUri;

        } else if (requestCode == CellSiteConstants.PICK_IDENTITY) {
            uri = data.getData();

        }

        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            Log.d(TAG, "filePath =" + filePath);
            Log.d(TAG, "uri=" + uri.toString());
            imageUri = Uri.fromFile(new File(filePath));
        } else //
        {
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                Toast.makeText(this, R.string.sdcard_occupied, Toast.LENGTH_SHORT).show();
                return;
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File tmpFile = new File(app.regUserPath + File.separator + "IMG_" + timeStamp + ".png");
            File srcFile = new File(uri.getPath());
            if (srcFile.exists()) {
                try {
                    Utils.copyFile(srcFile, tmpFile);
                    app.getUser().setIdentityImageUrl(tmpFile.getAbsolutePath());
                } catch (Exception e) {
                    Toast.makeText(this, R.string.create_tmp_file_fail, Toast.LENGTH_SHORT).show();
                    return;
                }
            } else {
                Log.d(TAG, "Logic error, should not come to here");
                Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
                return;
            }

            imageUri = Uri.fromFile(tmpFile);
        }

        Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null);
        Bitmap scaledBmp = Bitmap.createScaledBitmap(tmpBmp, CellSiteConstants.IDENTITY_IMAGE_WIDTH,
                CellSiteConstants.IDENTITY_IMAGE_HEIGHT, false);

        mUserIdentityIv.setImageBitmap(scaledBmp);
        // s isChanged = true;
        mUpdateImageTask = new UpdateImageTask();
        mUpdateImageTask.execute("" + app.getUser().getId(), Utils.bitmap2String(scaledBmp),
                CellSiteConstants.UPDATE_USER_IDENTITY_URL);

    } else if (requestCode == CellSiteConstants.TAKE_DRIVER_LICENSE
            || requestCode == CellSiteConstants.PICK_DRIVER_LICENSE) {

        Uri uri = null;
        if (requestCode == CellSiteConstants.TAKE_DRIVER_LICENSE) {
            uri = imageUri;

        } else if (requestCode == CellSiteConstants.PICK_DRIVER_LICENSE) {
            uri = data.getData();

        }

        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            Log.d(TAG, "filePath =" + filePath);
            Log.d(TAG, "uri=" + uri.toString());
            imageUri = Uri.fromFile(new File(filePath));
        } else //
        {
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                Toast.makeText(this, R.string.sdcard_occupied, Toast.LENGTH_SHORT).show();
                return;
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File tmpFile = new File(app.regUserPath + File.separator + "IMG_" + timeStamp + ".png");
            File srcFile = new File(uri.getPath());
            if (srcFile.exists()) {
                try {
                    Utils.copyFile(srcFile, tmpFile);
                    app.getUser().setDriverLicenseImageUrl(tmpFile.getAbsolutePath());
                } catch (Exception e) {
                    Toast.makeText(this, R.string.create_tmp_file_fail, Toast.LENGTH_SHORT).show();
                    return;
                }
            } else {
                Log.d(TAG, "Logic error, should not come to here");
                Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
                return;
            }

            imageUri = Uri.fromFile(tmpFile);
        }

        Bitmap tmpBmp = BitmapFactory.decodeFile(imageUri.getPath(), null);
        Bitmap scaledBmp = Bitmap.createScaledBitmap(tmpBmp, CellSiteConstants.IDENTITY_IMAGE_WIDTH,
                CellSiteConstants.IDENTITY_IMAGE_HEIGHT, false);

        mUserDriverLicenseIv.setImageBitmap(scaledBmp);
        // s isChanged = true;
        mUpdateImageTask = new UpdateImageTask();
        mUpdateImageTask.execute("" + app.getUser().getId(), Utils.bitmap2String(scaledBmp),
                CellSiteConstants.UPDATE_DRIVER_LICENSE_URL);

    }
}

From source file:com.skytree.epubtest.BookViewActivity.java

public Bitmap getBackgroundForLandscape() {
    Bitmap backgroundForLandscape = null;
    Theme theme = getCurrentTheme();/*from ww w.  jav  a2  s.c  o  m*/
    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    if (this.isDoublePagedForLandscape) {
        backgroundForLandscape = BitmapFactory
                .decodeFile(SkySetting.getStorageDirectory() + "/images/" + theme.doublePagedName, options);
    } else {
        backgroundForLandscape = BitmapFactory
                .decodeFile(SkySetting.getStorageDirectory() + "/images/" + theme.landscapeName, options);
    }
    return backgroundForLandscape;
}

From source file:com.skytree.epubtest.BookViewActivity.java

public Bitmap getBackgroundForPortrait() {
    Bitmap backgroundForPortrait;/* www .  j ava 2 s  .  com*/
    Theme theme = getCurrentTheme();
    Options options = new BitmapFactory.Options();
    options.inScaled = true;
    backgroundForPortrait = BitmapFactory
            .decodeFile(SkySetting.getStorageDirectory() + "/images/" + theme.portraitName, options);
    return backgroundForPortrait;
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w  w  w.  j  a v  a  2  s  .  c  o  m
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    Bitmap b = BitmapFactory.decodeFile(path, options);

    ExifInterface exif;
    try {
        exif = new ExifInterface(path);
    } catch (IOException e) {
        return null;
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    Matrix matrix = new Matrix();
    if (orientation == 6)
        matrix.postRotate(90);
    else if (orientation == 3)
        matrix.postRotate(180);
    else if (orientation == 8)
        matrix.postRotate(270);

    return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
}