Answer the question
In order to leave comments, you need to log in
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();
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
String myl = Base64.encodeToString(bytes, Base64.DEFAULT);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question