使用getCroppedImage时,带有emodo / cropper库的OutOfMemoryError

时间:2014-09-17 20:39:44

标签: android

我使用edmodo / cropper库在用户从相机拍摄图像后裁剪图像。 链接:https://github.com/edmodo/cropper/wiki

我在设备GT-N7000和其他一些Android手机上遇到了这个问题。

   java.lang.OutOfMemoryError
   1 at android.graphics.Bitmap.nativeCreate(Native Method)
   2 at android.graphics.Bitmap.createBitmap(Bitmap.java:669)
   3 at android.graphics.Bitmap.createBitmap(Bitmap.java:604)
   4 at android.graphics.Bitmap.createBitmap(Bitmap.java:530)
   5 at com.edmodo.cropper.CropImageView.getCroppedImage(CropImageView.java:357)

有谁知道如何解决这个问题。请帮帮我,设备不断崩溃。

2 个答案:

答案 0 :(得分:2)

我通过使用BitmapFactory.Options将它存储在cropView中之前对捕获的图像进行二次采样来解决它。

以下是代码:

    // setting path to the clicked image and cropped image
    path_click = "sdcard/Pictures/Candice/Clicked.jpg";
    path_crop = "sdcard/Pictures/Candice/Cropped.jpg";

    final BitmapFactory.Options options = new BitmapFactory.Options();
    //If set to a value > 1,requests the decoder to subsample the 
    //original image, returning a smaller image to save memory.
    options.inSampleSize = 2;

    clickedImage = BitmapFactory.decodeFile(path_click, options);
    cropImageView.setImageBitmap(clickedImage);

    // Sets initial aspect ratio to 10/10, for demonstration purposes
    cropImageView.setAspectRatio(DEFAULT_ASPECT_RATIO_VALUES,
            DEFAULT_ASPECT_RATIO_VALUES);

    cropButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Here we save the cropped image and then call the next
            // activity
            // To retrieve the image contained within the Cropper window,
            // use the provided method, getCroppedImage() to retrieve a
            // Bitmap of the cropped image.
            croppedImageBitmap = cropImageView.getCroppedImage();

            /** Save cropped image to SD card using output streams **/
            // An output stream that writes bytes to a file.
            // If it does not exist, a new file will be created.
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(path_crop);
                // Writing a compressed version of bitmap to outputstream.
                croppedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 90,
                        out);
                // Just after compression,add
                croppedImageBitmap.recycle();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            startActivity(chooseIntent);
        }
    });

答案 1 :(得分:0)

我不知道您的图片有多大,但是您是否尝试将android:largeHeap="true"添加到 AndroidManifest.xml 中的<application>标记?