How to implement the IM function in the app five quick implementation picture receive and send
Social app, user chat send or receive pictures, is a very common scene, here to introduce how to use Arrownock's Ansocial photo API and Anim combination, to achieve the sending and receiving pictures.
Send a feeling of love
When the sender chooses a good picture click Send, in order not to affect the user's experience during the chat, we usually insert the message into the SQLite database first, refresh the interface, and add loading animation to this message, indicating the transmission. And this behind the operation in order to be able to save the consumption of traffic, development, because the picture is often relatively large, so it is not directly the image of the binary data transmission past, but first compress the picture, and then transfer the past.
Specific implementation methods
Ansocial's photo API already offers a variety of arbitrarily compressed sizes, as an example of Android development, which can be called:
//assembly upload data created and compressed photo map<string, object> params = new HashMap<String, Object> (); inputstream Inputstream = context.getresources (). Getassets (). Open ("Testphoto.png"); // You need to use Ansocialfile ansocialfile file = new ansocialfile when uploading files (" Testphoto.jpg ", inputstream); params.put (" Photo ", file); //Assemble the size you want to compress Map<String, String> resolutions = new Hashmap<string,string> (); resolutions.put ("Small", "200x200"); resolutions.put ("Middle", "400x400"); params.put ("Resolutions", Resolutions); try { //Call Createphoto API Upload Images ansocial.sendrequest ("Photos/create.json", ansocialmethod.post, params, new iansocialcallback () { @Override public void onsuccess (JSONObject response ) { try { //upload image after successful, according to the returned data to get the original URL, medium map, small map jsonobject photo =response.getjsonobject ("Response") .getjsonobject ("Photo"); string fileurl =photo.getstring ("url"); string smallphotopath =photo.getjsonobject ("Resolutions") .getstring ("small"); string middlephotopath= photo.getjsonobject ("Resolutions") .getstring ("Middle"); //Assembly of the original, medium, small image of the url Map<String, String> Custommap = new hashmap<string,string> (); custommap.put ("SmallPhotoPath", Smallphotopath); custommap.put ("Middlephotopath", middlephotopath); custommap.put ( "url", fileurl); //because the image has been uploaded to the server, the chat message can only send an empty array byte[] b = new byte[1]; // Call Anim's Sendbinary method anim.sendbinary (clientid,b, "image", custommap); } catch (jsonexception e) { } } @Override public void onfailure (jsonobject arg0) { } }); } catch ( arrownockexception e) { }
As you can see from the code above, only the image address is sent when the chat message is sent. After sending the message successfully, refresh the interface and remove the loading animation.
Receiving a review:
When the receiver gets the message, it can get different picture sizes and display them in the interface from CustomData depending on the device and the screen size. Only when the user clicks on the picture will it jump to a new screen showing the image to load the original.
How to implement the IM feature series in the app article:
A common analysis of offline messages
The second fast realization of the offline message module
Three fast implementation of offline message push module
The four create a robust message sending module
The five rapid implementation of picture receiving and sending
The six fast realization of group chat advanced features
Seven fast implementation of chat list sorting module
This article from "Arrow Buckle technology Arrownock" blog, reproduced please contact the author!
How to implement the IM function in the app five quick implementation picture receive and send--arrow buckle technology Arrownock