Android Development How to load network images as thumbnails in micro-credit sharing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.