Problem in converting Photo Base64 on Android

0

The problem is the following, I take the photo right and I set it in the activity, so that's fine, but after converting it and sending it to the server and decoding, only only part of the photo appears:

Photo-taking code

private Integer ImageHeight = 520;
private Integer ImageWidth = 520;

            // Cria o caminho do arquivo no sdcard
            file = SDCardUtils.getPrivateFile(getBaseContext(), simpleDateFormat.format(new Date())+".jpg", Environment.DIRECTORY_PICTURES);

            // Chama a intent informando o arquivo para salvar a foto
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(i, 0);

Bitmap bitmap = ImageResizeUtils.getResizedImage(Uri.fromFile(file), ImageWidth, ImageHeight, true);

Foto = Utils.ConvertPhotoBase64(bitmap);

Base64 conversion code

public static String ConvertPhotoBase64(Bitmap bmp) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 50, baos);
    byte[] data = baos.toByteArray();
    Log.i("FOTO64", Base64.encodeToString(data, Base64.DEFAULT));
    return Base64.encodeToString(data, Base64.DEFAULT);
}

Thank you.

    
asked by anonymous 15.07.2016 / 16:35

1 answer

1

Try this approach below:

public static String getStringFromBitmap(Bitmap imagem){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    imagem.compress(Bitmap.CompressFormat.PNG, 30, baos); // bm is the bitmap

    byte[] photo = baos.toByteArray();
    return Base64.encodeToString(photo, Base64.NO_WRAP);
}
    
15.07.2016 / 17:57