0

I want to upload image to server which is accept images in base64 encored format. I use this code to encode bitmap, But it encode only half of the image and server receive only half of the image part.

This is my code.

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();

            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
// Send to server(encoded)

and when I try to encode image from gallery it pop up a error and crashes the app, with error.

java.lang.OutOfMemoryError: Failed to allocate a 15521174 byte allocation with 13777320 free bytes and 13MB until OOM

I want to encode the full image, any help?

KZoNE
  • 1,249
  • 1
  • 16
  • 27

3 Answers3

2

try this code:

 BitmapFactory.Options options;
 options = new BitmapFactory.Options();

    // downsizing image as it throws OutOfMemory Exception for larger
    // images
    options.inSampleSize = 6;
   Bitmap bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(),
                        options);
                ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
                if(bitmap1 != null) {
                    bitmap1.compress(Bitmap.CompressFormat.PNG, 10, baos1);
                    byte[] b1 = baos1.toByteArray();
                    bitmapstring1 = Base64.encodeToString(b1,    

                   Base64.DEFAULT);
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
  • Thanks for the answer, But I feel this will lower the quality of the image. Actually I don't want to make it low quality? Do you know any code to send with out lowering the quality? I mean, I think better to use bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos1); What do you think? – KZoNE Aug 24 '16 at 14:04
  • 1
    use it bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos1); no problem occured @KZoNE – Damini Mehra Aug 25 '16 at 09:51
1

you must be getting 'OutOfMemoryError' because may be you are using an image of very high resolution and loading it directly into the memory, and hence the String for base64 is too large. Always load a sub sampled version of the image in memory to avoid OutOfMemoryError. Take a look at this. From the docs:

For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.

First calculate the sample size for the image:

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;
}

Then decode the sampled version :

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);
}

After the sampled version of the bitmap is loaded, then convert the bitmap to base64.

Aman Grover
  • 1,621
  • 1
  • 21
  • 41
0
public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d(TAG, "getStringImage: "+encodedImage.trim());
    return encodedImage;
}

try this

UserSharma
  • 458
  • 6
  • 20