Problem in Converting Photo Base64 on Android

Asked

Viewed 495 times

0

The problem is this, I take the picture and if it in Activity, so far so good, but after converting it and sending to the server and decoding, only appears part of the photo:

Code to take the photo

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

Conversion code to Base64

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.

  • Will the problem is not in this resize 520x520?

  • Brother I have changed this to several sizes, and always the same thing, sometimes increases and decreases the size of the strokes after decoded. Another remark, when taking picture of a black part, at the time of converting error "Application stopped"

  • You have already tried for a different Thread, because android does not work in the same thread the image conversion, as well as access to WS. Have a look: http://stackoverflow.com/a/10594695/6405917

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);
}
  • I think your problem is in "Base64.DEFAULT".

  • continues the same thing brother, just slightly increased the amount of image in the photo.

  • Dude, weird, I use this approach and it works... Have you seen if the image comes right from the camera?

  • opa, I got here, the problem is that the Base64 Code was very big, and the BD field was text, I changed to long text and it worked, vlw by help!

  • Good time! Dude, are you saving the images in the bank? You could use the cache area of the app (a tip only hehehe), then avoid doing the Find and Code

  • Vlw, but it’s because I need to save on the server bro, I don’t know if it would be cool if I create the file on the server, have any suggestions?

  • It depends expensive, because at the end of the accounts, will traffic on the network in bytes...is that depends on the amount of photos and what type of database you have, as well as the relationship and access to the photos... If you depend too much on photos, and every time you send, need the photo and have database at will, use the database... If you have limited database, and have processing to turn the image into text every time, and have no dependency to send always...use photo.... Anyway, it depends hehehe

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.