//解决微信分享提示(**分享失败,分享多文件必须为图片格式**)问题
fun shareFiles(activity: Activity, list: List<String>) {
val uris = java.util.ArrayList<Uri>()
var type: String? = null
type = if (list[0].endsWith(".pdf")) {
"application/pdti"
} else if (list[0].endsWith(".xlsx") || list[0].endsWith(".xls")) {
"application/vnd.ms-excel"
} else if (list[0].endsWith(".doc") || list[0].endsWith(".docx")) {
"application/msword"
} else if (list[0].endsWith(".pptx") || list[0].endsWith(".ppt")) {
"application/vnd.ms-powerpoint"
} else if (list[0].endsWith(".jpg") || list[0].endsWith(".png")) {
"image/*"
} else if (list[0].endsWith(".txt")) {
"text/plain"
} else if (list[0].endsWith(".zip")) {
"application/zip"
} else {
"* / *"
}
for (i in list.indices) {
uris.add(Uri.fromFile(File(list[i])))
}
val multiple = uris.size > 1
val intent = Intent(if (multiple) Intent.ACTION_SEND_MULTIPLE else Intent.ACTION_SEND)
if (multiple) {
intent.type = type
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
} else {
intent.type = type
intent.putExtra(Intent.EXTRA_STREAM, uris[0])
}
sharedByApp(activity, 0, intent, 0)
}
private fun sharedByApp(context: Context, type: Int, intent: Intent, actResultcode: Int) {
try {
context.startActivity(
Intent.createChooser(
intent,
context.resources.getString(R.string.share)
)
)
} catch (e: Exception) {
}
}
文章仅用于学习笔记