1.依赖
//微信sdk
api'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
2.
private var wxApi:IWXAPI? =null
fun init(context:Context?) {
//实例化
wxApi =WXAPIFactory.createWXAPI(context,Constants.WX_APP_ID)
wxApi!!.registerApp(Constants.WX_APP_ID)
if (!wxApi!!.isWXAppInstalled()) {
Toast.makeText(context,"您还未安装微信客户端",Toast.LENGTH_SHORT).show()
return
}
}
/**
* bitmap保存为文件
*
* @param bm bitmap
* @param filePath 文件路径
* @return 返回保存结果 true:成功,false:失败
*/
fun saveBitmapToFile(bm:Bitmap, filePath:String):Boolean {
try {
val file = File(filePath)
file.deleteOnExit()
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
}
val bos = BufferedOutputStream(FileOutputStream(file))
var b =false
b =if (filePath.toLowerCase().endsWith(".png")) {
bm.compress(Bitmap.CompressFormat.PNG,100,bos)
}else {
bm.compress(Bitmap.CompressFormat.JPEG,100,bos)
}
bos.flush()
bos.close()
return b
}catch (e:FileNotFoundException) {
}catch (e:IOException) {
}
return false
}
// 判断微信版本是否为7.0.13及以上
fun checkVersionValid(context:Context?):Boolean {
return wxApi!!.getWXAppSupportAPI() >=0x27000D00
}
// 判断Android版本是否7.0及以上
fun checkAndroidNotBelowN():Boolean {
return Build.VERSION.SDK_INT >=Build.VERSION_CODES.N
}
fun getFileUri(context:Context, file:File?):String? {
if (file ==null || !file.exists()) {
return null
}
val contentUri:Uri =FileProvider.getUriForFile(context,
"com.gialen.shop.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://"开头的用于共享的路径
}
/**
* 图片分享
*/
@JvmStatic
fun shareImage(context:Context?, bitmap:Bitmap, type:Int) {
init(context)
if (checkVersionValid(context) && checkAndroidNotBelowN()) {
// 使用contentPath作为文件路径进行分享
val filePath = context!!.getExternalFilesDir(null).toString() +"/shareData/" +System.currentTimeMillis().toString() +".png"
val saveBitmap = saveBitmapToFile(bitmap,filePath)
if (saveBitmap) {
val file = File(filePath)
val fileUri = getFileUri(context,file)
val imageObj = WXImageObject()
imageObj.setImagePath(fileUri)
val msg = WXMediaMessage()
msg.mediaObject =imageObj
bitmap.recycle()
val req =SendMessageToWX.Req()
req.transaction = buildTransaction("img")
req.message =msg
if (type ==0) {
req.scene =SendMessageToWX.Req.WXSceneSession
}else if (type ==1) {
req.scene =SendMessageToWX.Req.WXSceneTimeline
}
wxApi!!.sendReq(req)
}
}else {
// 使用原有方式传递文件路径进行分享
val imageObj = WXImageObject(bitmap)
val msg = WXMediaMessage()
msg.mediaObject =imageObj
bitmap.recycle()
val req =SendMessageToWX.Req()
req.transaction = buildTransaction("img")
req.message =msg
if (type ==0) {
req.scene =SendMessageToWX.Req.WXSceneSession
}else if (type ==1) {
req.scene =SendMessageToWX.Req.WXSceneTimeline
}
wxApi!!.sendReq(req)
}
}
3
根据Android官方给出的适配方案,在主工程的AndroidManifest.xml 中增加 <queries> 标签,即可解决以上影响,代码如下:
<manifestpackage="com.example.app">...
// 在应用的AndroidManifest.xml添加如下<queries>标签
<queries>
<packageandroid:name="com.tencent.mm"/>// 指定微信包名
</queries>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
...
</manifest>
4.file_provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="sharedata" path="shareData/" />
</paths>