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

Android 码拜 9年前 (2015-04-25) 878次浏览 0个评论


(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;
	}
android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
10分
一般情况下出现在哪个位置?
android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
引用 1 楼 u013280307 的回复:

一般情况下出现在哪个位置?

这个我也不清楚,因为上面那个方法是我修改过的了,行数都已经乱了。大概是if (angle != 0) { 这里 }

android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
。。。不会吧   一个判断就oom了?
android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
Bitmap bitmap = Utils.comp(BitmapFactory.decodeFile(filePath),MainCompileActivity.width,MainCompileActivity.height);
这个地方通过BitmapFactory.decodeFile(filePath)这个方法,当图片稍微大一点的时候,加载出来的bitmap就非常大,消耗大量的内存,然后接下来你对这个bitmap做旋转,内存肯定溢出了,建议你查看下bitmap的大小,看是不是这个问题。如果是的话,那就改变下获取bitmap的方式或者加个Option
android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
引用 4 楼 u010514380 的回复:

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

那这里应该怎么改呀?

android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
30分
引用 5 楼 baidu_23478311 的回复:
Quote: 引用 4 楼 u010514380 的回复:

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;  
    }


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!