I want to send the image byte array to the subscriber through the mqtt broker. But the size of the image byte array data is too big to be published on the mqtt broker, so how can I send the image byte array data to the subscriber?
PicBitmap = ((BitmapDrawable)iVpic.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PicBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
picbyte = bos.toByteArray();
String s = Base64.encodeToString(picbyte,Base64.DEFAULT);
String pichex = toHexString(s);
String payload = pichex;
byte[] encodedPayload = new byte[0];
try {
encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
message.setQos(qos);
mqttClient.publish(topic, message);
} catch (UnsupportedEncodingException | MqttException e) {
e.printStackTrace();
}
public static String toHexString(String input) {
return String.format("%x", new BigInteger(1, input.getBytes()));
}
I need to convert the byte array to string and hex ascii code first then only publish on the broker. But the string converted from byte arary is too long, it failed to be published everytime i try to publish it.
I do not believe your actual image is bigger than 256mb the max payload size for a MQTT message. A 12 megapixel jpeg is only about 3.5mb, a lossless PNG will be about 7mb. (source)
Base64 encoding the images will increase it's size by approximately 1/3. This is not needed as MQTT messages are just a stream of bytes so there is no need to encode the image. (this would still be only 9.3mb)
Your
toHexString()
function is just doubling your payload for no benefit. The output of the base64 encode is already a string, converting each of the bytes (represented by 1 char) that represents that string into 2 chars (in the range 0-9a-f) does nothing useful. (still only 18.6mb)I have edited out all the unneeded code, if you still have a problem ask a new question and include the stacktrace from the
e.printStackTrace()
output so we can see what the real issue is.