OutOfMemoryError with image selection in Android. How to resize image before decoding it to bitmap?

后端 未结 5 1628
深忆病人
深忆病人 2021-01-16 17:52

I\'m building an imagepicker in my Android app, for which I used the example code on this page. This basically gives me a button which opens the possibility to get a file fr

相关标签:
5条回答
  • 2021-01-16 18:29

    Basically, you should use BitmapFactory.decodeFile(path, options) passing Bitmap.Options.inSampleSize to decode a subsampled version of the original Bitmap.

    Your code will look something like this:

    public Bitmap decodeBitmap(String path, int maxWidth, int maxHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.inJustDecodeBounds = false;
        options.inSampleSize = calculateInSampleSize(maxWidth, maxHeight, options.outWidth, options.outHeight);
        return BitmapFactory.decodeFile(path, options);
    }
    

    And the calculateInSampleSize code:

    private int calculateInSampleSize(int maxWidth, int maxHeight, int width, int height) {
        double widthRatio = (double) width / maxWidth;
        double heightRatio = (double) height / maxHeight;
        double ratio = Math.min(widthRatio, heightRatio);
        float n = 1.0f;
            while ((n * 2) <= ratio) {
                n *= 2;
            }
            return (int) n;
        }
    
    0 讨论(0)
  • 2021-01-16 18:37

    try this i have used it once . it worked for me.

    private Bitmap getBitmap(String path) {
    
        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
            in = mContentResolver.openInputStream(uri);
    
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, o);
            in.close();
    
    
    
            int scale = 1;
            while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
                  IMAGE_MAX_SIZE) {
               scale++;
            }
            Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
               orig-height: " + o.outHeight);
    
            Bitmap b = null;
            in = mContentResolver.openInputStream(uri);
            if (scale > 1) {
                scale--;
                // scale to max possible inSampleSize that still yields an image
                // larger than target
                o = new BitmapFactory.Options();
                o.inSampleSize = scale;
                b = BitmapFactory.decodeStream(in, null, o);
    
                // resize to desired dimensions
                int height = b.getHeight();
                int width = b.getWidth();
                Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
                   height: " + height);
    
                double y = Math.sqrt(IMAGE_MAX_SIZE
                        / (((double) width) / height));
                double x = (y / height) * width;
    
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
                   (int) y, true);
                b.recycle();
                b = scaledBitmap;
    
                System.gc();
            } else {
                b = BitmapFactory.decodeStream(in);
            }
            in.close();
    
            Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
               b.getHeight());
            return b;
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(),e);
            return null;
        }
    
    0 讨论(0)
  • 2021-01-16 18:45

    While you load large bitmap files, BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.).

    STEP 1

    Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;
    

    To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it.

    STEP 2

    To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object.

    For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384. Loading this into memory uses 0.75MB rather than 12MB for the full image.

    Here’s a method to calculate a sample size value that is a power of two based on a target width and height:

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {
    
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
    }
    
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, 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;
    }
    

    Please read this link for details. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    0 讨论(0)
  • 2021-01-16 18:48

    You can use image sampling which is loading the smaller size image without having to load the bigger one. You can do that with Options argument passed to BitmapFactory. Set Options.inJustDecodeBounds true and BitmapFactory will set Options.outWidth and Options.outHeight fields to image's original size. Note that when setting inJustDecodeBounds to true the returned Bitmap is null. Also use Options.inSampleSize to decode sampled image. Sample size shows how much the result image is scaled down.

    0 讨论(0)
  • 2021-01-16 18:49

    to avoid this error,aquire large heap using this in manifest:

    android:largeHeap="true"
    

    then decode bitmap, re-size according to your need

    0 讨论(0)
提交回复
热议问题