我有一个旋转图像的方法,但我总是收到一个OutMemoryError,但我在相册中的图像是从相机拍摄的,尺寸宽度是5000~手机
我将照片调整为宽度1280和高度960
public static Boolean ShowImagesCapture(Context context,Uri PATH_IMAGE,ImageCropView view,int width,int height){
int orientation=0;
Boolean success = true;
try {
Bitmap bitmap =null;
if (Build.VERSION.SDK_INT < 19) {
String selectedImagePath = getPath(PATH_IMAGE,context);
bitmap = BitmapFactory.decodeFile(selectedImagePath);
orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));
}
else {
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE,"r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
orientation=GetPhotoOrientation(context,PATH_IMAGE));
} catch (Exception e) {
e.printstacktrace();
}
}
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap=rotateBitmap(bitmap,3,width,height);
view.setimageBitmap(bitmap);
break;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap=rotateBitmap(bitmap,8,height);
view.setimageBitmap(bitmap);
break;
case ExifInterface.ORIENTATION_TRANsveRSE:
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap=rotateBitmap(bitmap,6,height);
view.setimageBitmap(bitmap);
break;
default:
view.setimageBitmap(bitmap);
}
bitmap = null;
}
catch (Exception e) {
e.printstacktrace();
success= false;
}
System.gc();
return success;
}
我的旋转图像方法是
public static Bitmap rotateBitmap(Bitmap bitmap,int orientation,int height) {
try {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_norMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
// matrix.setScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
// matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
// matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANsveRSE:
matrix.setRotate(-90);
// matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-270);
break;
default:
return bitmap;
}
Bitmap bmRotated = Bitmap.createBitmap(bitmap,height,matrix,false);
bitmap.recycle();
return bmRotated;
}
catch (OutOfMemoryError e) {
Log.e(TAG,"Out memory Error");
return null;
}catch (Exception e){
e.printstacktrace();
return null;
}
}
我的错误在哪里?
* ——————- ** 2016年6月27日更新** ——————- *
public static Bitmap rotateBitmap(Bitmap bitmap,int height) {
try {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_norMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANsveRSE:
matrix.setRotate(-90);
matrix.postScale(-1,1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-270);
break;
default:
return bitmap;
}
Bitmap bmRotated= null;
try {
Bitmap tmp_bitmap= Bitmap.createScaledBitmap(bitmap,true);
bmRotated = Bitmap.createBitmap(tmp_bitmap,tmp_bitmap.getWidth(),tmp_bitmap.getHeight(),true);
bitmap.recycle();
}catch (OutOfMemoryError e){
e.printstacktrace();
}
return bmRotated;
} catch (Exception e){
e.printstacktrace();
return null;
}
}
public static Bitmap decodefilebitmap(String selectedImagePath,int reqWidth,int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(selectedImagePath,options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options,int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfheight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfheight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
//MetoDO Para MOSTRAR LA IMAGEN DESDE LA galERIA
public static Boolean ShowImagesCapture(Context context,int height){
int orientation=0;
Boolean success = true;
try {
Bitmap bitmap =null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
if (Build.VERSION.SDK_INT < 19) {
String selectedImagePath = getPath(PATH_IMAGE,context);
bitmap = decodefilebitmap(selectedImagePath,bitmap.getWidth(),bitmap.getHeight());
orientation=GetPhotoOrientation(context,PATH_IMAGE));
} catch (Exception e) {
e.printstacktrace();
}
}
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap=rotateBitmap(bitmap,height);
view.setimageBitmap(bitmap);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap=rotateBitmap(bitmap,height);
view.setimageBitmap(bitmap);
break;
default:
view.setimageBitmap(bitmap);
}
bitmap = null;
}
catch (Exception e) {
e.printstacktrace();
success= false;
}
System.gc();
return success;
}
最佳答案
那是因为你将整个位图加载到内存bitmap = BitmapFactory.decodeFile(selectedImagePath);然后在ImageView中显示一个调整大小的版本(但由于你在RAM中有完整大小的版本而浪费内存).您需要加载缩小版本.加载所有位图然后对其进行操作(缩放,旋转,将其放入图像视图)与将该位图的缩小版本加载到内存中是不一样的.例如,如果您有5000 x 5000像素的图像,则假设JPEG格式的大小约为1MB.但是当您将其加载到内存中时,您将其解压缩并加载该图像的整个未压缩版本.假设你以每像素32位加载它,那么它在RAM中的大小将是5000x5000x32位,即大约95MB!所以你需要加载缩小版本.查看此Android开发人员文档about loading a scaled down bitmap version into memory.这将有助于您更好地了解问题.您还可以使用像Glide这样的图像加载库.这些库可以完成所有这些以及更多.
原文地址:https://www.jb51.cc/android/430014.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。