android 中关于出现内存不足错误的有关问题,求大神帮小弟我看看,多谢啦

android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!

(java.lang.OutOfMemoryError)
请问大神们,下面方法代码中,还能怎么优化?因为这里有时会报内存不足错误!
求支招!谢谢!


/**
 * 通过ExifInterface类读取图片文件的被旋转角度,并且纠正。
 * @param context
 * @param u 图片文件的路径
 * @return
 */
public static Bitmap getExifOrientation(Context context, Uri u) {
Bitmap bm = null;
//不管是拍照还是选择图片每张图片都有在数据中存储也存储有对应旋转角度orientation值
    Cursor cursor = context.getContentResolver().query(u, null, null, null, null);//根据Uri从数据库中查找  
    if (cursor != null && cursor.moveToNext()) {
        String filePath = cursor.getString(cursor.getColumnIndex("_data"));//获取图片路径
        String orientation = cursor.getString(cursor.getColumnIndex("orientation"));//获取图片旋转的角度  
        cursor.close();
        if (filePath != null) {
            try {
Bitmap bitmap = Utils.comp(BitmapFactory.decodeFile(filePath),MainCompileActivity.width,MainCompileActivity.height);//根据Path读取资源图片  
int angle = 0;//角度
if (orientation != null && !"".equals(orientation)) {
angle = Integer.parseInt(orientation);
}
if (angle != 0) {
//纠正图片的角度
final Matrix m = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
m.setRotate(angle);
bm = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);
} else {
bm = bitmap;
}
} catch (Exception e) {
bm = null;
Log.e("===>>>", "抛出异常");
}
        }else{
         bm = null;
        }
    }else{
     bm = null;
    }
    return bm;
}

------解决思路----------------------
一般情况下出现在哪个位置?
------解决思路----------------------
引用:
Quote: 引用:

Bitmap bitmap = Utils.comp(BitmapFactory.decodeFile(filePath),MainCompileActivity.width,MainCompileActivity.height);
这个地方通过BitmapFactory.decodeFile(filePath)这个方法,当图片稍微大一点的时候,加载出来的bitmap就非常大,消耗大量的内存,然后接下来你对这个bitmap做旋转,内存肯定溢出了,建议你查看下bitmap的大小,看是不是这个问题。如果是的话,那就改变下获取bitmap的方式或者加个Option


那这里应该怎么改呀?


给你一个方法,你试试,不行的话我还有一个,哈哈哈
/** 
     * 创建Heigh*width分辨率的图片 
     * @param filePath 
     * @return 
     */  
    public static Bitmap createImage(String filePath,int width,int heigh) {  
        Bitmap bitmap = null;  
        BitmapFactory.Options opts = new BitmapFactory.Options();  
        opts.inJustDecodeBounds = true;  
        BitmapFactory.decodeFile(filePath, opts);  
  
        opts.inSampleSize = computeSampleSize(opts, -1, heigh * width);  
        opts.inJustDecodeBounds = false;  
  
        try {  
            bitmap = BitmapFactory.decodeFile(filePath, opts);  
        } catch (Exception e) {  
            // TODO: handle exception 
         e.printStackTrace();
        }  
        return bitmap;  
    }