X
X
xepy2020-03-29 18:52:12
Java
xepy, 2020-03-29 18:52:12

Passing image from android to mysql and back?

Hello, I am writing a simple chat on Android Studio.

Text information (author, message text, addressee) is driven into a HashMap, converted into a byte array. I write to the output stream. Nothing out of the ordinary here:

try {
            URL url_link = new URL("адрес обработчика данных на сайте");

            HttpURLConnection my_connect = (HttpURLConnection) url_link.openConnection();

            my_connect.setRequestMethod("POST");

            my_connect.setReadTimeout(5000);
            my_connect.setConnectTimeout(5000);

            my_connect.setDoOutput(true);
            my_connect.setDoInput(true);

            StringBuilder stringbuilder = new StringBuilder();

            HashMap<String, String> map_array = new HashMap<String, String>();

            map_array.put("user_from", user_from);
            map_array.put("user_to", user_to);
            map_array.put("text_m", text_m);
            map_array.put("img_m", img_m);           //            тут проблема
            map_array.put("create", "yes");


            for (Map.Entry<String, String> p_data : map_array.entrySet()) {
                stringbuilder.append(p_data.getKey() + "=" + p_data.getValue() + "&");
            }

            byte[] byte_arr = stringbuilder.toString().getBytes("UTF-8");

            OutputStream my_outs = my_connect.getOutputStream();

            my_outs.write(byte_arr);
            my_connect.connect();
}


Now it's time for the images. I launch the intent with the camera, take a picture, write the photo to a file, at the end I get a bitmap

And I didn’t understand how to proceed further.

1) I tried to encode a string in base64 and send it to the server, saving it immediately to the muscle. Of course, I realized that it was stupid. The string is gigantic, when sent back to android, it takes much more than 64kb and does not fit into string.

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] bytes = byteArrayOutputStream.toByteArray();

String myl = Base64.encodeToString(bytes, Base64.DEFAULT);


2) Or do I need to decode base64 on the server, then do something with it and save it? Maybe you need to somehow create a photo already on the server from a string using PHP GD?

3) Or do I need to transfer Bitmap directly somehow?

Tell me, dear experts, where to dig, there are many options, the information on the Internet is so-so, I don’t want to spend days working through each option. Maybe there are better solutions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-03-29
@firedragon

Do POST, encode in the usual way described in RFC
encoding="multipart/form-data"
This is the direct way.
The second option is to use WebSocket and send binary data through it, as well as data in your chat.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question