android 中关于出现内存不足错误的有关问题,求大神帮小弟我看看,多谢啦
android 中关于出现内存不足异常的问题,求大神帮我看看,谢谢啦!
(java.lang.OutOfMemoryError)
请问大神们,下面方法代码中,还能怎么优化?因为这里有时会报内存不足错误!
求支招!谢谢!
------解决思路----------------------
一般情况下出现在哪个位置?
------解决思路----------------------
给你一个方法,你试试,不行的话我还有一个,哈哈哈
/**
* 创建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;
}
(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;
}
------解决思路----------------------
一般情况下出现在哪个位置?
------解决思路----------------------
给你一个方法,你试试,不行的话我还有一个,哈哈哈
/**
* 创建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;
}