Converting a bitmap to a byte array, consuming a lot of memory

I need to convert a bitmap to a byte array. To upload to the server, I was able to achieve the same by converting Bitmap to a byte array and converting it to Base 64 String again with the following instructions , it worked fine here, but in the worst case in my s2 mobile galaxy, if the image size is 6 MB with a resolution of 72 pixels / inch, it takes about 600 MB of RAM and the application crashes with OutOfMemoryException, I tried to load the bitmap compression worked fine, but in my project requirement I need to load the image, that is, without any compression of the original and siderations

please help me how to achieve this, is it possible or not.

Thanks at Advance

+4
source share
2 answers

To load conversion to jpeg stream

BufferedStream bs = //...

then call bitmap.compress("JPEG", 0, length, bs)

convert bs to ad upload array, which is to the server

0
source

The heap of application memory for application applications is limited. especially on low memory devices.

you make two different common mistakes:

  • : - Bitmap (, ). . - ( ImageView) :

    public static Bitmap getSampleBitmapFromFile(String bitmapFilePath, int reqWidth, int reqHeight) {
    try {
        File f = new File(bitmapFilePath);
        ExifInterface exif = new ExifInterface(f.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
        int angle = 0;
    
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle = 270;
        }
    
        Matrix mat = new Matrix();
        mat.postRotate(angle);
    
        // calculating image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, options);
    
        int scale = calculateInSampleSize(options, reqWidth, reqHeight);
    
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
    
        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        Bitmap correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
    
        return correctBmp;
    } catch (IOException e) {
        Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with IO exception: ", e);
        if (e != null)
            e.printStackTrace();
    } catch (OutOfMemoryError oom) {
        Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with OOM error: ");
        if (oom != null)
            oom.printStackTrace();
    } catch (Exception e) {
        Log.e(TAG, "cant butmaputils.getSampleBitmapFromFile failed with exception: ", e);
    }
    Log.e(TAG, "butmaputils.getSampleBitmapFromFilereturn null for file: " + bitmapFilePath);
    return null;
     }
    
    
    
    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) {
    
        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
    
        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    
    return inSampleSize;
    }
    

- , () 1080x1920 100x200, imageView.

  • second: : , , Bitmap +, 64 . - .

, , 100M-1000M - .

, - http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

0

Source: https://habr.com/ru/post/1532598/


All Articles