It is quite common to sneak into and share wechat friends or circle of friends in App. For example, Android shares a web page information (URL) to the code of wechat client:

/** * wechat share: * @param context * @param URL * @param title * @param description * @param scene */ public static 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 yet install WeChat 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); }Copy the code

Although it has been used for many times in different apps, there was still a mistake in a recent project: after executing this code, the application did not respond, the wechat client could not be activated, and there was no error message printing prompt. I had to look at the official Android FAQ and find this:

Q: Call wxapi.sendReq interface and return true, but wechat client is not started. Please check the following items: 1) Whether wechat is installed 2) Whether the Apk package name and signature are consistent with those filled in on the open platform, please use this tool for signature: 3) Check whether the thumbnail size exceeds 32K when you send it. 4) You can adjust wechat to select friends list, but there is no response after clicking send. Please check whether the proGuard configuration is confused with the wechat SDK code. It is recommended not to confuse the SDK pair. Refer to the following ProGuard configuration:

-keep class com.tencent.mm.sdk.** {
   *;
}Copy the code

Upon inspection, the code iwXapi.sendreq (req) was found; After execution, false is returned. In fact, according to the Q&A written above, it is not the scope of the problem. After checking the four points, the local preview size of the sent thumbnail is less than 20KB, and there is no problem with other configurations, but it still fails. What is the problem?

Entanglements, meditation, almost doubt life! Finally, in a spirit of giving it a try, I replaced the thumbnail image with a smaller one that was less than 7KB, executed the code again, and found a surprising result: iWXapi.Sendreq (req); Returns true and successfully initiates the wechat client! At that time in the heart of ten thousand grass mud horse pentium ah!

After some excitement, I began to study why the thumbnail I used before did not exceed the 32K limit of the official website document, but I could not activate the wechat client. Could it be that the official website document was written wrong and the upper limit was not 32KB? So go back to the source code, open the class WXMediaMessage provided by wechat SDK, and find a series of constants defined as follows:

public static final int THUMB_LENGTH_LIMIT = 32768;

private static final int TITLE_LENGTH_LIMIT = 512;

private static final int DESCRIPTION_LENGTH_LIMIT = 1024;

private static final int MEDIA_TAG_NAME_LENGTH_LIMIT = 64;

private static final int MESSAGE_ACTION_LENGTH_LIMIT = 2048;

private static final int MESSAGE_EXT_LENGTH_LIMIT = 2048;Copy the code

Sure enough, the wechat SDK limits the size of thumbnails, title length, description length and other information shared to wechat. Among them, the thumbnail size is limited to 32768, the source code does not note the unit. Curious, I divide it by 1024, which gives me 32 bytes. That means there’s nothing wrong with the official documentation, but what’s wrong with it?

In fact, it is about the actual size of the hard disk and memory of the image. Image files stored on a computer’s hard drive are compressed according to compression rules for different image formats, such as common lossy image formats such as JPEG, to reduce disk usage. On Android, the amount of memory used to read images into memory has nothing to do with the actual size of the image stored on the hard disk. It may be larger or smaller, but you can use the following code to obtain the size of the image:

private Bitmap decodeResource(Resources 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;
}Copy the code

Bitmap.getrowbytes () * bitmap.getheight () displays the memory usage of the bitmap in bytes, which can be converted into BK units by dividing it by 1024. Note: DecodeResource (Resources Res, int ID) to retrieve a Bitmap from a resource without using the decodeResource(Resources Res, int ID) method. In order to avoid the image stored in different drawable or Mipmap folder caused by inconsistent memory usage problem, Android screen adaptation friends should know this, I will not go into detail here, For more information, see Keyzig’s article on the relationship between image size, memory footprint and Drawable folders in Android.

Modify the thumbnail size through PS tool, and then test the memory size occupied by different sizes of pictures in Android phone through the above code, and check whether the wechat client can be adjusted. After such tests, it was finally found that the maximum size of 32KB thumbnail in wechat SDK and official documents refers to the memory footprint, rather than the disk footprint of the image. In this way, I solved the problem I encountered in front.

Finally, we have to make fun of the criticism of Android wechat SDK, which is also a common problem of some third-party service providers, including Alipay SDK. There is no other purpose but to vent:

  • Signature uniqueness Android developers know that apK files compiled and packaged to run on the phone or emulator use the default generic signature provided by the IDE during development, while apK files released officially use the official signature file defined by the developer. Wechat SDK can only enter one signature information when registering an application, so it is necessary to test the related functions of wechat SDK in the official package, and the official package cannot track and debug, which is very inconvenient. Of course, you can also do this. During the development stage, you can register the signature information of the test package on the wechat open platform, and then change it into the formal signature file information when it goes online. Or you can modify the IDE’s default signature file. But these are not very convenient, if wechat open platform can like some other third-party service providers, for an application to provide two or more signature information registration, will not be too bad.

  • The document is not clear Many of the major function of the third party service provider may provide, whatever documentation, even the Samples written code is at sixes and sevens, lead us to the developer in use process doesn’t even have a complete reference, out of the problem is also a hindrance, wasted a lot of unnecessary time and effort.

The article is a little wordy, mainly describes their own development of wechat sharing problems encountered, analysis of the problem and solve the problem process, I hope to give you some reference, but also welcome your attention, exchange and share, learn from each other, common progress.

Friendship is recommendedFRIENDLY


Focus on programmer programming knowledge, original tutorials, the latest developments and so on. This is the golden age of programming, ShowTime for programmers. We’re not coders, we’re not losers, we’re not coders. We’re coders

Wechat ID: ProgrammerLeague

Review images

Popular choice




☞ Every mobile developer should have their own App

☞ Android Studio uses Gradle to introduce a summary of third-party library files

☞ Programmer benefits | Recommend free interfaces for some well-known platforms

☞ How to write a resume? These two templates are enough!

▌ Pokemon GO is now available in China!