The _android and thinking of recording the Android micro-credit sharing function

Source: Internet
Author: User
Tags documentation memory usage

Sneak into the app to share the functions of a micro-trust friend or circle of friends is a common occurrence, such as Android sharing a Web page information (URL) to the micro-client code:

 /** * Micro-credit sharing: Sharing Web page * @param context * @param URL * @param title * @param description * @param scene/public Stati  c void Sharetowechatwithwebpage (context context, string URL, string title, string description, int scene) {Iwxapi Iwxapi
 = Wxapifactory.createwxapi (context, wxentryactivity.wxapi_app_id); if (!iwxapi.iswxappinstalled ()) {toastmanager.getinstance (Context.getapplicationcontext ()). ShowToast ("You have not installed a micro-client
 ");
 Return
 } wxwebpageobject wxwebpageobject = new Wxwebpageobject ();
 Wxwebpageobject.webpageurl = URL;
 Wxmediamessage wxmediamessage = new Wxmediamessage (wxwebpageobject);
 Wxmediamessage.mediaobject = Wxwebpageobject;
 Wxmediamessage.title = title;
 wxmediamessage.description = description; Wxmediamessage.thumbdata = Imagemanager.bmptobytearray (Bitmapfactory.decoderesource (Context.getResources (),
 R.mipmap.ic_share_invite), true);
 Sendmessagetowx.req Req = new Sendmessagetowx.req ();
 Req.transaction = string.valueof (System.currenttimemillis ()); Req.message= Wxmediamessage;
 Req.scene = scene;
 Iwxapi.sendreq (req); 
 }

Although it has been used in different apps for n times, it has been a mistake in a recent project: After executing this code, there is no response to the application, the micro-client cannot be tuned, and no error messages are printed. Have to check the official information--[android FAQ] and find this tip:

Q: Call the Wxapi.sendreq interface, return true, but the micro-client does not start, please check the following:
A:
1) whether the micro-letter is installed
2 if the APK package name and signature are in accordance with the open platform, the signature please use the tool: Click to download, often occurs in the installation of the debug version and install release version of the situation, determine the package name signature Uninstall micro-letter reload or clear micro-letter data and then do the test
3) Check whether the thumbnail size is over 32k when sent
4 can adjust the micro-letter to select a friend list, but click Send no response, please check the Proguard configuration of the micro-letter SDK code is confused, we do not recommend to the SDK to confuse, refer to the following Proguard configuration:
-keep class com.tencent.mm.sdk.** {
*;
}

After inspection, found code iwxapi.sendreq (req); After the execution returned false, in fact, according to the above Q&a, no longer belong to the problem category. But still follow these four points to check, the thumbnail sent to the local preview size of less than 20KB, the other configuration is no problem, but still wrong, in the end what is the problem?
Tangle, meditation, almost suspected of life! Finally at the risk of trying the attitude, I put the thumbnail into a small map of less than 7KB, execute the code again, the results were strikingly found:iwxapi.sendreq (req); Returns true and the micro-client is successfully tuned! At that time, 10,000 grass-mud horses in the heart Pentium!
After some excitement, began to study, why the previous use of the thumbnail is not more than the official website document 32K limit, but can not adjust the micro-letter client, is not the official website document wrong, the upper limit is not 32KB? Then return to the source code, open the micro-trust SDK provided by the class Wxmediamessage, find the following defined a series of constants:

public static final int thumb_length_limit = 32768;
private static final int title_length_limit =;
private static final int description_length_limit = 1024;
private static final int media_tag_name_length_limit =;
private static final int message_action_length_limit = 2048;
private static final int message_ext_length_limit = 2048; 

Sure enough, the micro-mail SDK is limited to sharing information about the thumbnail size, caption length, and description length of the micro-letter. Among them, the thumbnail size limit is 32768, the source code does not note the unit. Curious I divide it by 1024, just get 32, this is not the official website documents mentioned in the upper limit of 32KB (description of the source code in the number of units in byte)! That is to say that the official website document is not wrong, but where is the problem?
Actually it's about the actual hard disk size and memory footprint of the picture. The image files stored on the computer's hard disk will be compressed according to the compression rules of the different picture formats, thus reducing the size of the hard disk, such as the lossy compression image format such as JPEG. In Android, the amount of memory used to read an image to memory has little to do with the actual size of the picture on the hard disk, and may be larger or smaller, using the following code to get the amount of memory the image occupies:

 Private Bitmap Decoderesource (resources, int id) {
typedvalue value = new Typedvalue ();
Resources.openrawresource (ID, value);
Bitmapfactory.options opts = new Bitmapfactory.options ();
opts.intargetdensity = value.density;
Bitmap Bitmap = Bitmapfactory.decoderesource (resources, ID, opts);
LOG.I ("Bitmap", "size is" + bitmap.getrowbytes () * bitmap.getheight ());
return bitmap;
} 

whichbitmap.getrowbytes () * Bitmap.getheight ( )Gets the bitmap memory footprint, in bytes, and then divided by 1024, which can be converted to BK units. Note: The above process of getting the bitmap object from the resource is not directly usedDecoderesource (resources res, int id)The method with two parameters is to avoid the inconsistency of memory usage caused by the pictures stored in different drawable or mipmap folders, and friends who know about the Android screen adaptation should understand this, and here is not to elaborate, you can refer to an article of the Boston Brothers--[ About Android image size, memory footprint and Drawable folder relationship research and analysis.
Using the PS tool, modify the thumbnail size, and then test the size of the memory used by different sizes of images on the Android phone, and see if the micro-client can be tuned. This test led to the discovery that the 32KB thumbnail upper limit size in the micro-mail SDK and official documentation refers to memory footprint, not to the size of the hard disk used by the picture. In this way, it also solves the problems I have encountered before.
Finally, you'll have to spit out the blame for the Android micro-trust SDK, as well as some of the common issues with other Third-party service providers in the Alipay SDK, just to vent:
• Signature Uniqueness
Android developers know that the APK files compiled and packaged in the development process and run on the phone or emulator use the default universal signature provided by the IDE, while the APK file that is published online uses the developer's custom signed file. Micro-letter SDK in the registration of applications can only enter a signature information, resulting in the formal package must be tested in the micro-letter SDK related functions, and the formal package is not able to track debugging, very inconvenient. Of course, you can do this, in the development phase, in the micro-trust platform to register the test package signature information, on the line to modify the official signature file information; or you can modify the IDE's default signature file. But these are not very convenient, if the micro-credit open platform can be like some other third-party service providers, for an application to provide two or more signed information registration, not fast.
• The document is not clear
Many large third-party service providers just function to provide, regardless of the documentation, or even the samples of the code are written in a mess, leading to our developers in the use of the process even a complete reference is not, out of the problem can not start, wasted a lot of unnecessary time and energy.

The article is some long-winded, mainly elaborated oneself this time to develop the micro-letter to share the question, the analysis question and solves the problem the process, hoped gives everybody some reference.

Related Article

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.