One of the projects I've been doing these days is to share a micro-letter feature that will add titles, descriptions, links, and thumbnails to share. Let's take a look at the official API documentation:
Web page type Sharing example:
Initializes a Wxwebpageobject object and fills in the URL
Wxwebpageobject webpage = new Wxwebpageobject ();
Webpage.webpageurl = "Web url";
Initializes a Wxmediamessage object with the Wxwebpageobject object, fills in the title, describes
Wxmediamessage msg = new Wxmediamessage (webpage);
Msg.title = "page title";
msg.description = "Web description";
Bitmap thumb = Bitmapfactory.decoderesource (Getresources (), r.drawable.send_music_thumb);
Msg.thumbdata = Util.bmptobytearray (thumb, true);
Construct a req
Sendmessagetowx.req Req = new Sendmessagetowx.req ();
Req.transaction = buildtransaction ("webpage");//transaction field is used to uniquely identify a request
Req.message = msg;
Req.scene = istimelinecb.ischecked ()? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
Call API interface to send data to micro-mail
Api.sendreq (req);
The thumbnail setting is this line of code:
Msg.thumbdata= a bitmap
Visible, the official API only provides examples of loading local resource pictures, does not give the load network image as a thumbnail sample code, of course, it is easy to achieve, as long as we convert the network image to bitmap objects on it, but when I use the custom bitmap time, but always error, And the error does not give any hint information, finally know the micro-letter sharing thumbnail (thumb) max 64KB, incredibly have such a limit, so had to do some processing. All implementation code is as follows:
Network picture converted to bitmap object code:
/**
* Transform the network resource picture into bitmap
* @param URL network resource picture
* @return Bitmap
*/
public static Bitmap getlocalornetbitmap (String URL) {
Bitmap Bitmap = null;
InputStream in = null;
Bufferedoutputstream out = null;
try {
in = new Bufferedinputstream (new URL (URL). OpenStream (), 1024);
Final Bytearrayoutputstream DataStream = new Bytearrayoutputstream ();
out = new Bufferedoutputstream (DataStream, 1024);
Copy (in, out);
Out.flush ();
byte[] data = Datastream.tobytearray ();
Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length);
data = null;
return bitmap;
catch (IOException e) {
E.printstacktrace ();
return null;
}
}
private static void Copy (InputStream in, outputstream out)
Throws IOException {
Byte[] B = new byte[1024];
int read;
while (read = In.read (b))!=-1) {
Out.write (b, 0, read);
}
}
Then change the two lines in the example code to read as follows:
Bitmap Thumb =bitmap.createscaledbitmap ("A network resource picture"), Bitmap, true);
Msg.thumbdata = Util.bmptobytearray (thumb, true);
Problem solving, successful implementation of micro-letter sharing.