Android 11 WeChat, QQ, Weibo sharing adaptation

Android 11 WeChat, QQ, Weibo sharing adaptation

Preface

Recently, I received feedback from customer service and some users feedback that WeChat can’t share. I asked for a specific question and found that it was a Xiaomi machine with Android 11. Then I tried QQ Weibo and it didn’t work, so I started to adapt. The latest Android 11 has partitioned storage. When I upgraded to tagsdk=30, I also tried it and found that many third parties are not working, so I used tagsdk=29
FileProvider official website adaptation instructions to help understand the meaning of the relevant code: https://developer.android.google .cn/reference/kotlin/androidx/core/content/FileProvider

Insert picture description here

WeChat articles

The official WeChat description of the relevant connection is as follows: https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/Android.html#jump2
Insert picture description here
Then it is useless, there are still problems!

Then I found an address on the official website:
OpenSDK supports FileProvider to share files to WeChat official: https://developers.weixin.qq.com/community/develop/doc/0004886026c1a8402d2a040ee5b401
Insert picture description here
This time it’s right, follow suit Can

important point

1.String filePath = context.getExternalFilesDir(null) + "/shareData/test.png";
// This filePath corresponds to the first line of configuration in xml/file_provider_paths:, so it can be shared.
Here is a line of explanation, in the corresponding The following path in the xml configuration should be written in the first line

<external-files-path name="sharedata" path="shareData/"/>

2. Convert the path to content://URI format through the FileProvider interface. The description here will cause the sharing to be a black image because of incorrect writing or file processing problems
Insert picture description here

That is, if you share the microcredit is: mWXImageObject.imagePath = xxxxx;
this method, then you need to pay attention that the path must be in the form of content://URI obtained above, of course, this path needs to store the image, otherwise the file is empty, and the sharing is a black screen
Insert picture description here

public static String dealAndroid11FileWechat(Context context, Bitmap mBmp, File fileFilePri) {
    
    
        try {
    
    
            if (mBmp == null) {
    
    
                return null;
            }

            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileFilePri));
            mBmp.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
            outputStream.flush();
            outputStream.close();

            // 微信使用contentPath作为文件路径进行分享
            return CommonUtils.getWeiChartFileUri(context, fileFilePri);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

        return null;
    }
public static String getWeiChartFileUri(Context context, File file) {
    
    
        if (file == null || !file.exists()) {
    
    
            return null;
        }

        Uri contentUri = FileProvider.getUriForFile(context,
                context.getPackageName() + ".fileprovider",  // 要与`AndroidManifest.xml`里配置的`authorities`一致,假设你的应用包名为com.example.app
                file);

        // 授权给微信访问路径
        context.grantUriPermission("com.tencent.mm",  // 这里填微信包名
                contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

        return contentUri.toString();   // contentUri.toString() 即是以"content://"开头的用于共享的路径
    }

Related instructions, the above two methods, checkVersionValid and checkAndroidNotBelowN, are included in the official WeChat document, so they are not listed. The first method is to store the partition cache file, and the second method is to get the path at the beginning of content. I added one here. The variable storage of android11Path, make sure `mWXImageObject.imagePath = android11Path. As for other non-partitioned file storage, it is still the previous logic. When you finally share, you can judge by yourself whether the imagePath starts with content or is a non-partitioned file path. Go to WeChat Android 11 The sharing process is complete.

Because the logic of the respective sharing integration is inconsistent, I will not post the specific sharing code. In the existing logic, go to Android11Path, or the previous path, which is really not good: sharing is abnormal, Android 11 and above systems need to use FileProvider to share WeChat sharing Failed (do not talk about the code), please refer to: https://www.jianshu.com/p/05c83a83d876
for specific codes.

QQ articles

Sharing function storage permission adaptation official document: https://wiki.connect.qq.com
Insert picture description here
QQ adaptation is very smooth, there is no big problem, praise! , FileProvider and WeChat xml pay attention to the added position

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${PACKAGE_NAME}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

Weibo articles

sinaweibosdk/weibo_android_sdk,github 地址:https://github.com/sinaweibosdk/weibo_android_sdk

Insert picture description here
The specific adaptation has a pdf document under this directory, and the adaptation is still very smooth, because I don’t have multiple pictures to share, Weibo multi-picture sharing, the following logic, but I did not see the relevant configuration information in the document getExternalFilesDir(null) This is very embarrassing in a little configuration description! ! ! !

if (mShareMultiImage.isChecked()) {
    
    
            // 分享多图
            MultiImageObject multiImageObject = new MultiImageObject();
            ArrayList<Uri> list = new ArrayList<>();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
    
                String authority = this.getPackageName() + ".fileprovider";
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/aaa.png")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/ccc.JPG")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/ddd.jpg")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/fff.jpg")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/ggg.JPG")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/eee.jpg")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/hhhh.jpg")));
                list.add(FileProvider.getUriForFile(this, authority, new File(getExternalFilesDir(null) + "/kkk.JPG")));
            } else {
    
    
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/aaa.png")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/ccc.JPG")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/ddd.jpg")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/fff.jpg")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/ggg.JPG")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/eee.jpg")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/hhhh.jpg")));
                list.add(Uri.fromFile(new File(getExternalFilesDir(null) + "/kkk.JPG")));
            }
            multiImageObject.imageList = list;
            message.multiImageObject = multiImageObject;
        }

Guess you like

Origin blog.csdn.net/android_freshman/article/details/113122078