package com.anguomob.total.utils
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import androidx.core.content.FileProvider
import com.anguomob.total.R
import java.io.File
object ShareUtils {
/**
* 分享我自己的app
* @param activity
*/
fun shareMyApp(activity: Activity) {
// 清单文件需要配置
val f = File(activity.packageResourcePath)
//调用android分享窗口
val intent = Intent()
intent.action = Intent.ACTION_SEND
if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
val apkUri =
FileProvider.getUriForFile(activity, activity.packageName + ".fileprovider", f)
//添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_STREAM, apkUri)
} else {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f))
}
intent.type = "*/*"
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
activity.startActivity(intent)
}
fun shareVideo(context: Context, filePath: String) {
shareFile(context, filePath, "video/*")
}
fun shareImage(context: Context, filePath: String) {
shareFile(context, filePath, "image/*")
}
fun shareFile(context: Context, filePath: String, fileType: String = "*/*") {
if (TextUtils.isEmpty(filePath)) {
ToastUtils.showShort(R.string.file_path_not_exist)
return
}
val file: File = File(filePath)
if (!file.exists()) {
ToastUtils.showShort(R.string.file_path_not_exist)
return
}
val intent = Intent(Intent.ACTION_SEND)
intent.type = fileType
val fileUri = FileProvider.getUriForFile(
context, context.getPackageName().toString() + ".fileprovider",
file
)
intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
}
<application
android:requestLegacyExternalStorage="true"
android:networkSecurityConfig="@xml/ag_network_config"
ag_network_config
<?xml version="1.0" encoding="utf-8"?>
<network-security-config xmlns:tools="http://schemas.android.com/tools">
<base-config cleartextTrafficPermitted="true" />
<domain-config>
<domain includeSubdomains="true">i.snssdk.com</domain>
<domain includeSubdomains="true">is.snssdk.com</domain>
<domain includeSubdomains="true">extlog.snssdk.com</domain>
<domain includeSubdomains="true">sf3-ttcdn-tos.pstatp.com</domain>
<domain includeSubdomains="true">bds.snssdk.com</domain>
<domain includeSubdomains="true">dig.bdurl.net</domain>
<trust-anchors>
<certificates src="user"/>//信任用户自己安装的证书
<certificates src="system"/>
</trust-anchors>
</domain-config>
</network-security-config>