I want in my app send photo image to the server. API uses Base64 format of images. I have path to the image/photo and now I want convert this path to the Base64 array.
For load the image I have this code (it´s also resize the image to max size) from here:
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;
}
And this is code to send the image:
        image = getBitmap(pathToImage);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        String filenameArray[] = fileName.split("\\.");
        String extension = filenameArray[filenameArray.length - 1];
        if (extension.equalsIgnoreCase("jpg")
                || extension.equalsIgnoreCase("jpeg"))
            image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        else
            image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        image.recycle();
        image = null;
        byte[] byteArray = stream.toByteArray();
        try {
            stream.close();
        } catch (IOException e1) {
        }
        stream = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream(
                (int) (byteArray.length * 1.5));
        JsonGenerator jgenerator = null;
        try {
            jgenerator = new JsonFactory().createGenerator(baos);
            jgenerator.writeStartObject();
            jgenerator.writeStringField("fileName", fileName);
            // Jackson takes care of the base64 encoding for us
            jgenerator.writeBinaryField("content", byteArray);
            jgenerator.writeEndObject();
            jgenerator.close();
        } catch (JsonGenerationException e) {
        } catch (IOException e) {
        }
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Globals.URL + "/storageUploadFile");
        httppost.setHeader("Token", Globals.Token);
        httppost.setHeader("Content-type",
                "application/json; charset=utf-8");
        httppost.setEntity(new ByteArrayEntity(baos.toByteArray()));
        HttpResponse response;
        try {
            response = httpClient.execute(httppost);
            HttpEntity httpentity = response.getEntity();
            msg = EntityUtils.toString(httpentity);
            jgenerator = null;
            httppost = null;
            httpClient = null;
            httpentity = null;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
Sometimes I get an OutOfMemory error in row Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true); or in row jgenerator.writeBinaryField("content", byteArray);
Can you help me to modify the code to avoid OutOfMemory error? Do you have some tips how to convert image from file to Base64 and send it as HTTPPost?
 
     
     
    