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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…