Y
Y
Yaroslav Vorontsov2016-12-11 19:30:13
Java
Yaroslav Vorontsov, 2016-12-11 19:30:13

Ways to convert image to base64 in java?

There is a certain ImageView in which the image lies. It is required to transfer the image to the server using the POST method. On the server, the data is processed by a php script.

img = photoHolder.getDrawable();
BitmapDrawable bitmapDrawable = ((BitmapDrawable) img);
Bitmap bitmap = bitmapDrawable .getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte = stream.toByteArray();

And somewhere below
String img = new String(Base64.encode(imageInByte, Base64.DEFAULT));
String myParams = "img="+img;

Everything seems to work ... But the images do not open. Obviously, I'm converting to base64 incorrectly somehow..
But just in case, here's the image processing code
$img1 = base64_decode($img);
$filename = "img/asd.jpg";
file_put_contents($filename, $img1);

What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-12-11
@ChipoDeil

So encode the same image on the server and compare, maybe the problem is just in the encoding, not in the one that is base64, but in the one in which the bytes are presented in the request - UTF-8 or 1251, start with something simpler, better with plain text files of several bytes.
Actually, your question is...
- So, now show me how you learned to turn off the computer ... Yes, not with RESET, but with the mouse!
- Good! *picks up mouse from table and clicks on RESET*

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question