Android Bitmap Load decodeFile(File f, int size)

Here you can find the source of decodeFile(File f, int size)

Description

See: http://stackoverflow.com/questions/477572/android-strange -out-of-memory-issue-while-loading-an-image -to-a-bitmap-object/823966#823966 Thanks to StackOverflow user named Fedor.

License

Open Source License

Declaration

public static Bitmap decodeFile(File f, int size) 

Method Source Code

//package com.java2s;
/*// w  w w.jav a2s.com
 * Ubuntu One Files - access Ubuntu One cloud storage on Android platform.
 * 
 * Copyright 2011-2012 Canonical Ltd.
 *   
 * This file is part of Ubuntu One Files.
 *  
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses 
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    /**
     * See: http://stackoverflow.com/questions/477572/android-strange
     *       -out-of-memory-issue-while-loading-an-image
     *       -to-a-bitmap-object/823966#823966
     * Thanks to StackOverflow user named Fedor.
     */
    public static Bitmap decodeFile(File f, int size) {
        Bitmap b = null;
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            FileInputStream fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();

            int scale = 1;
            if (o.outHeight > size || o.outWidth > size) {
                scale = (int) Math.pow(
                        2.0,
                        (int) Math.round(Math.log(size
                                / (double) Math
                                        .max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inTempStorage = new byte[32 * 1024];
            o2.inPurgeable = true;
            o2.inSampleSize = scale;
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
        } catch (IOException e) {
        }
        return b;
    }
}

Related

  1. loadImage(String filename)
  2. loadImage(String urlStr)
  3. loadPreviewBitmap(String fileName, int width, int height)
  4. SafeDecodeBitmapFile( String strFilePath)
  5. decodeF(File f)
  6. decodeFromDescriptor( FileDescriptor descriptor)
  7. decodeFromDescriptor( FileDescriptor descriptor, int reqWidth, int reqHeight)
  8. getBitmap(Context c, String uri, int minsizeDP)
  9. getImageFromAssetFile(Context context, String fileName)