java – Android镜像调整大小错误内存

前端之家收集整理的这篇文章主要介绍了java – Android镜像调整大小错误内存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个旋转图像的方法,但我总是收到一个OutMemoryError,但我在相册中的图像是从相机拍摄的,尺寸宽度是5000~手机

我将照片调整为宽度1280和高度960

我的第一个显示和调整图像的方法

  1. public static Boolean ShowImagesCapture(Context context,Uri PATH_IMAGE,ImageCropView view,int width,int height){
  2. int orientation=0;
  3. Boolean success = true;
  4. try {
  5. Bitmap bitmap =null;
  6. if (Build.VERSION.SDK_INT < 19) {
  7. String selectedImagePath = getPath(PATH_IMAGE,context);
  8. bitmap = BitmapFactory.decodeFile(selectedImagePath);
  9. orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));
  10. }
  11. else {
  12. ParcelFileDescriptor parcelFileDescriptor;
  13. try {
  14. parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE,"r");
  15. FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
  16. bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
  17. parcelFileDescriptor.close();
  18. orientation=GetPhotoOrientation(context,PATH_IMAGE));
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. switch (orientation) {
  24. case ExifInterface.ORIENTATION_ROTATE_180:
  25. bitmap=rotateBitmap(bitmap,3,width,height);
  26. view.setImageBitmap(bitmap);
  27. break;
  28. break;
  29. case ExifInterface.ORIENTATION_ROTATE_90:
  30. bitmap=rotateBitmap(bitmap,8,height);
  31. view.setImageBitmap(bitmap);
  32. break;
  33. case ExifInterface.ORIENTATION_TRANSVERSE:
  34. break;
  35. case ExifInterface.ORIENTATION_ROTATE_270:
  36. bitmap=rotateBitmap(bitmap,6,height);
  37. view.setImageBitmap(bitmap);
  38. break;
  39. default:
  40. view.setImageBitmap(bitmap);
  41. }
  42. bitmap = null;
  43. }
  44. catch (Exception e) {
  45. e.printStackTrace();
  46. success= false;
  47. }
  48. System.gc();
  49. return success;
  50. }

我的旋转图像方法

  1. public static Bitmap rotateBitmap(Bitmap bitmap,int orientation,int height) {
  2. try {
  3. Matrix matrix = new Matrix();
  4. switch (orientation) {
  5. case ExifInterface.ORIENTATION_NORMAL:
  6. return bitmap;
  7. case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
  8. // matrix.setScale(-1,1);
  9. break;
  10. case ExifInterface.ORIENTATION_ROTATE_180:
  11. matrix.setRotate(180);
  12. break;
  13. case ExifInterface.ORIENTATION_FLIP_VERTICAL:
  14. matrix.setRotate(180);
  15. // matrix.postScale(-1,1);
  16. break;
  17. case ExifInterface.ORIENTATION_TRANSPOSE:
  18. matrix.setRotate(90);
  19. // matrix.postScale(-1,1);
  20. break;
  21. case ExifInterface.ORIENTATION_ROTATE_90:
  22. matrix.setRotate(90);
  23. break;
  24. case ExifInterface.ORIENTATION_TRANSVERSE:
  25. matrix.setRotate(-90);
  26. // matrix.postScale(-1,1);
  27. break;
  28. case ExifInterface.ORIENTATION_ROTATE_270:
  29. matrix.setRotate(-270);
  30. break;
  31. default:
  32. return bitmap;
  33. }
  34. Bitmap bmRotated = Bitmap.createBitmap(bitmap,height,matrix,false);
  35. bitmap.recycle();
  36. return bmRotated;
  37. }
  38. catch (OutOfMemoryError e) {
  39. Log.e(TAG,"Out memory Error");
  40. return null;
  41. }catch (Exception e){
  42. e.printStackTrace();
  43. return null;
  44. }
  45. }

我的错误在哪里?

* ——————- ** 2016年6月27日更新** ——————- *

我的代码有一个最好的版本工作

  1. public static Bitmap rotateBitmap(Bitmap bitmap,int height) {
  2. try {
  3. Matrix matrix = new Matrix();
  4. switch (orientation) {
  5. case ExifInterface.ORIENTATION_NORMAL:
  6. return bitmap;
  7. case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
  8. matrix.setScale(-1,1);
  9. break;
  10. case ExifInterface.ORIENTATION_ROTATE_180:
  11. matrix.setRotate(180);
  12. break;
  13. case ExifInterface.ORIENTATION_FLIP_VERTICAL:
  14. matrix.setRotate(180);
  15. matrix.postScale(-1,1);
  16. break;
  17. case ExifInterface.ORIENTATION_TRANSPOSE:
  18. matrix.setRotate(90);
  19. matrix.postScale(-1,1);
  20. break;
  21. case ExifInterface.ORIENTATION_ROTATE_90:
  22. matrix.setRotate(90);
  23. break;
  24. case ExifInterface.ORIENTATION_TRANSVERSE:
  25. matrix.setRotate(-90);
  26. matrix.postScale(-1,1);
  27. break;
  28. case ExifInterface.ORIENTATION_ROTATE_270:
  29. matrix.setRotate(-270);
  30. break;
  31. default:
  32. return bitmap;
  33. }
  34. Bitmap bmRotated= null;
  35. try {
  36. Bitmap tmp_bitmap= Bitmap.createScaledBitmap(bitmap,true);
  37. bmRotated = Bitmap.createBitmap(tmp_bitmap,tmp_bitmap.getWidth(),tmp_bitmap.getHeight(),true);
  38. bitmap.recycle();
  39. }catch (OutOfMemoryError e){
  40. e.printStackTrace();
  41. }
  42. return bmRotated;
  43. } catch (Exception e){
  44. e.printStackTrace();
  45. return null;
  46. }
  47. }
  48. public static Bitmap decodefilebitmap(String selectedImagePath,int reqWidth,int reqHeight) {
  49. // First decode with inJustDecodeBounds=true to check dimensions
  50. final BitmapFactory.Options options = new BitmapFactory.Options();
  51. options.inJustDecodeBounds = true;
  52. BitmapFactory.decodeFile(selectedImagePath,options);
  53. // Calculate inSampleSize
  54. options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
  55. // Decode bitmap with inSampleSize set
  56. options.inJustDecodeBounds = false;
  57. return BitmapFactory.decodeFile(selectedImagePath,options);
  58. }
  59. public static int calculateInSampleSize(
  60. BitmapFactory.Options options,int reqHeight) {
  61. // Raw height and width of image
  62. final int height = options.outHeight;
  63. final int width = options.outWidth;
  64. int inSampleSize = 1;
  65. if (height > reqHeight || width > reqWidth) {
  66. final int halfHeight = height / 2;
  67. final int halfWidth = width / 2;
  68. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  69. // height and width larger than the requested height and width.
  70. while ((halfHeight / inSampleSize) > reqHeight
  71. && (halfWidth / inSampleSize) > reqWidth) {
  72. inSampleSize *= 2;
  73. }
  74. }
  75. return inSampleSize;
  76. }
  77. //METODO PARA MOSTRAR LA IMAGEN DESDE LA GALERIA
  78. public static Boolean ShowImagesCapture(Context context,int height){
  79. int orientation=0;
  80. Boolean success = true;
  81. try {
  82. Bitmap bitmap =null;
  83. BitmapFactory.Options options = new BitmapFactory.Options();
  84. options.inJustDecodeBounds = true;
  85. if (Build.VERSION.SDK_INT < 19) {
  86. String selectedImagePath = getPath(PATH_IMAGE,context);
  87. bitmap = decodefilebitmap(selectedImagePath,bitmap.getWidth(),bitmap.getHeight());
  88. orientation=GetPhotoOrientation(context,PATH_IMAGE));
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. switch (orientation) {
  94. case ExifInterface.ORIENTATION_ROTATE_180:
  95. bitmap=rotateBitmap(bitmap,height);
  96. view.setImageBitmap(bitmap);
  97. break;
  98. case ExifInterface.ORIENTATION_ROTATE_90:
  99. bitmap=rotateBitmap(bitmap,height);
  100. view.setImageBitmap(bitmap);
  101. break;
  102. default:
  103. view.setImageBitmap(bitmap);
  104. }
  105. bitmap = null;
  106. }
  107. catch (Exception e) {
  108. e.printStackTrace();
  109. success= false;
  110. }
  111. System.gc();
  112. return success;
  113. }
最佳答案
那是因为你将整个位图加载到内存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这样的图像加载库.这些库可以完成所有这些以及更多.

猜你在找的Android相关文章