简体   繁体   English

将图库图像加载到应用程序中,加载第5或第6张图片后出现内存错误

[英]Loading gallery image into app, Out Of Memory Error after 5th or 6th picture loaded

I am constantly getting Out Of Memory Errors after loading 5 or 6 gallery images into my app. 将5或6个图库图像加载到我的应用程序后,我经常Out Of Memory Errors

I am calling bitmap.recycle() after every image is done loading so wondering why I am still getting OOM error? 我在每个图像完成加载后调用bitmap.recycle()所以想知道为什么我仍然会收到OOM错误?

This is how I handle gallery images: 这是我处理图库图像的方式:

  1. press button from inside app that goes to gallery to select image 从应用程序内部按下按钮进入图库以选择图像
  2. goes back to app, and copies the gallery image to app's external files directory 返回应用程序,并将库图像复制到应用程序的外部文件目录
  3. compress and rotate the image 压缩和旋转图像
  4. load image into ImageView 将图像加载到ImageView中

     public static void compressAndRotateImage(Context context, String filename) { try { File file = new File(context.getExternalFilesDir(null), filename); // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(file), null, o); // The new size we want to scale to final int REQUIRED_SIZE = 720; // Find the correct scale value. It should be the power of 2. int scale = 1; while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) { scale*=2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(file), null, o2); // Get orientation of picture ExifInterface exif = null; try { exif = new ExifInterface(context.getExternalFilesDir(null) + "/" + filename); } catch (IOException e1) { e1.printStackTrace(); } int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1); Log.i("ORIENTATION", Integer.toString(orientation)); // Rotate image to portrait based on taken orientation Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); } else if (orientation == 3) { matrix.postRotate(180); } else if (orientation == 8) { matrix.postRotate(270); } bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); // Save and compress file FileOutputStream fos = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 90, fos); try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } bm.recycle(); System.gc(); } catch (FileNotFoundException e) { e.printStackTrace(); } 

    } }

    Copies gallery image to app external files dir 将图库图像复制到应用程序外部文件目录

     private void copyGalleryImage(Uri uri) { String realPath = ImageHandler.getRealPathFromUri(this, uri); File galleryFile = new File(realPath); imgFilename = ImageHandler.getImageFilename(this); File file = new File(getExternalFilesDir(null), imgFilename); GlobalMethods.copyFile(galleryFile, file); Log.i("IMAGE COPIED TO INTERNAL", imgFilename); ImageHandler.compressAndRotateImage(this, imgFilename); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); NewPictureFragment fragment = NewPictureFragment.newInstance(imgFilename); ft.replace(R.id.fragment_container, fragment); ft.addToBackStack(null); ft.commit(); } 

Use android:largeHeap="true" in Manifest File. 在清单文件中使用android:largeHeap="true" Maybe this solve your problem. 也许这可以解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM