Android 将图片文件,转成Bitmap

from stackoverflow
解决 Out of memory 问题
private Bitmap decodeFile(File f){
   
Bitmap b = null;
   
try {
       
//Decode image size
       
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 > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale
= Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
       
}

       
//Decode with inSampleSize
       
BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2
.inSampleSize = scale;
        fis
= new FileInputStream(f);
        b
= BitmapFactory.decodeStream(fis, null, o2);
        fis
.close();
   
} catch (FileNotFoundException e) {
   
}
   
return b;
}

你可能感兴趣的:(android,image,File,null)