I am developing an Android application base on chat demo application done by Firebase.
The problem I'm facing is when I want to transfer images, I don't know how to handle the situation.
for sending the image I encode it in Base64 and send it as a string. So how will be the process of decoding to the image again here?
you must try this.
byte[] imageAsBytes = Base64.decode(String,Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0,imageAsBytes.length);
Related
We are trying to call a post request via HTTP call in android, the library that we are using is Retrofit.
First of all, we encode our Bitmap image to base64 with the following code.
public static String toBase64(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}
And then we send our data using Retrofit to our server.
Our backend is written both with Spring and Flask frameworks and both servers behave exactly the same.
We've tried to debug and find the problem. We found that some weird character is added to the request's body sometimes. It is not persistent but sometimes we got errors on server side.
As you can see in the picture some characters are replaced with some weird characters that aren't in UTF-8 charset. Also, we have this issue on iOS platforms too and I should mention that the size of the image is around 1MB.
The 'Content-Type' header is set correctly like content-type: application/json; UTF-8.
I would really appreciate it if someone could help.
I managed to get a node to communicate to an android app. The message goes from the node to the gateway. Using node-red the gateway publishes the message to an mqtt broker and I subscribe to the right topic to receive that message on my android app.
Only problem I have now is that I never realized that the msg was encoded and I'm quite lost now. I receive an array of bytes on the app side and I'm not sure what is the next step I have to take to be able to read that message.
Thanks for the help!
You are actually receiving bytes from Node-Red. You could either convert the received bytes into String on your Android or modify your lora input node's data type from Bytes to UTF-8.
I have a function to decode it:
var x = Buffer.from(msg.payload.raw,"base64").toString()
msg.Decrypted = parseFloat(x)
return msg;
In this Instructable i explained how to decode Lora messages in node red
https://www.instructables.com/id/Lora-Temperature-Dashboard/
On a MultiTech Conduit, the message is base64 encoded by the internals of the MTC, then published to the internal mqtt broker at lora//up, so you need to base64 decode it, then do whatever you need to with it. You can use the built in mosquitto applications to subscribe to this topic, or any other mqtt client (paho libraries, etc)
I am trying to upload image fromandroid to asp.net server.
I am following following procedure.
convert image from bitmap to byte[] yhen byte[] to string and pass this string to the asp.net web service
is this correct way to save image on to the .net server
Please Give the solution to upload image from android to asp.net server
both client and server side code .
The simplest way is convert your bitmap to base64 string :
public String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
after converting ,you can send the string to webservice, on the server side, you must decode this string to an image !
To convert base64 to image on server side,duckduckgo or google will help you :
https://duckduckgo.com
https://www.google.com/search?sclient=psy-ab&site=&source=hp&btnG=Search&q=convert+base64+to+image+ASp.net+Server
Have a look at this tutorial by Microsoft - you'll be able to use the Build the Web Service part for the web service.
For the Android part, you can get inspired by all other questions that try to send a POST request - for example this one.
If not too late, here is my suggested solution:
Server side: you can create a web service or RESTful application on the .NET Framework. Microsoft provides ASP.NET Web API. You can start from their website here
Client side: your Android here. You can refer to some of the following: Volley, Retrofit...
If you want to use Volley, you can refer to some following links:
Working POST Multipart Request with Volley and without HttpEntity
How to send a “multipart/form-data” POST in Android with Volley
Trouble Sending Multipart File with Boundary via Volley
Of course, you can find more available in SO.
Hope this helps!
In android I am trying to send image to the server.I am using Multipart request. But when I add image in format ByteArray to the Multipart UnsupportedEncodingException occurs.
following is my code.
Do I need to add mime type to request.
If possible please post the complete Sample code.
Try converting the Image to Base64 format string and then sending that string to the server.
Here's the link to convert image to base64 format.
And then at server end decode it.
I done it with the use of MultiPartEntity . Added image as byteArrayBody and done the HTTP request.
First of all I am using a client-server architecture, android for the client and node.js for the server, they are connected through Socket.io library, so, they are using websockets.
The doubt that I have is that I am generating a XML string with XMLSerializer from Java, I want to encode it to EXI and send it to a server, thus, is it possible doing the encode XML-EXI without using files? directly from string to string? because all examples I see assume that my XML is in a file and I want the output into another file. Another doubt is, can I just send the EXI as string? because I have already established the communication between the client and the server, but they just send strings, I don not if I can sent whole files, in that case, would be any diference on the amount of data sent?
Finally I have solved it, for people with the same problem, the solution is:
String input = methodGivingXMLString();
byte inputBytes[] = input.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(inputBytes);
transmogrifier.encode(new InputSource(in));
For the input, and for the output:
ByteArrayOutputStream result = new ByteArrayOutputStream();
transmogrifier.setOutputStream(result);
note 1: I am using OpenExi library
note 2: The output stream has to be set before calling the encode() method.