Android系统自带分享图片功能
作者:_wiky_ 时间:2022-08-03 09:14:07
简介
记录一个利用系统分享功能进行图片分享的工具类(代码是用Kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出)。调用方式比较简单:
Util.startShareImage(this) //this为当前的Activity实例
权限
记得添加文件操作权限, 另外需要注意6.0版本以上的权限管理
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
具体细节见代码
/**
* 系统分享图片功能
* Created by wiky on 2018/1/13.
*/
object Util {
fun startShareImage(activity: Activity) {
//过滤出需要分享到对应的平台:微信好友、朋友圈、QQ好友。 可自行修改
val targetApp = arrayOf("com.tencent.mm.ui.tools.ShareImgUI", "com.tencent.mm.ui.tools.ShareToTimeLineUI", "com.tencent.mobileqq.activity.JumpActivity")
/** * 分享图片 */
val bitmap = getImageFromAssetsFile(activity, "img_share.jpg") //从assets目录中取到对应的文件,文件名自行修改
val localImage = saveBitmap(bitmap!!, "share.jpg") //分享前,需要先将图片存在本地(记得添加权限),文件名自行修改
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*" //设置分享内容的类型:图片
shareIntent.putExtra(Intent.EXTRA_STREAM, localImage)
try {
val resInfo = activity.packageManager.queryIntentActivities(shareIntent, 0)
if (!resInfo.isEmpty()) {
val targetedShareIntents = ArrayList<Intent>()
for (info in resInfo) {
val targeted = Intent(Intent.ACTION_SEND)
targeted.type = "image/*" //设置分享内容的类型
val activityInfo = info.activityInfo
//如果还需要分享至其它平台,可以打印出具体信息,然后找到对应的Activity名称,填入上面的数组中即可
// println("package = ${activityInfo.packageName}, activity = ${activityInfo.name}")
//进行过滤(只显示需要分享的平台)
if (targetApp.any { it == activityInfo.name }) {
val comp = ComponentName(activityInfo.packageName, activityInfo.name)
targeted.component = comp
targeted.putExtra(Intent.EXTRA_STREAM, localImage)
targetedShareIntents.add(targeted)
}
}
val chooserIntent = Intent.createChooser(targetedShareIntents.removeAt(0), "选择要分享到的平台")
if (chooserIntent != null) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toTypedArray<Parcelable>())
activity.startActivity(chooserIntent)
}
}
} catch (e: Exception) {
Log.e(StatConstants.LOG_TAG, "Unable to share image, logs : " + e.toString())
}
}
/** * 从Assets中读取图片 */
private fun getImageFromAssetsFile(context: Context, fileName: String): Bitmap? {
var image: Bitmap? = null
val am = context.resources.assets
try {
val inputStream = am.open(fileName)
image = BitmapFactory.decodeStream(inputStream)
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
return image
}
/** * 将图片存到本地 */
private fun saveBitmap(bm: Bitmap, picName: String): Uri? {
try {
val dir = Environment.getExternalStorageDirectory().absolutePath + File.separator + picName
val f = File(dir)
if (!f.exists()) {
f.parentFile.mkdirs()
f.createNewFile()
}
val out = FileOutputStream(f)
bm.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
out.close()
return Uri.fromFile(f)
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
}
来源:https://blog.csdn.net/cai_iac/article/details/79217715
标签:Android,分享图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中解决EditText放到popupWindow中,原有复制、粘贴、全选、选择功能失效问题
2021-07-12 10:51:49
![](https://img.aspxhome.com/file/2023/8/139358_0s.png)
c#调用存储过程实现登录界面详解
2023-01-02 22:03:07
![](https://img.aspxhome.com/file/2023/5/80125_0s.gif)
Java 微信公众号开发相关总结
2021-11-11 03:43:30
![](https://img.aspxhome.com/file/2023/8/88048_0s.png)
ANDROID中自定义对话框AlertDialog使用示例
2023-07-22 17:24:12
![](https://img.aspxhome.com/file/2023/8/139068_0s.gif)
Java将Date日期类型字段转换成json字符串的方法
2023-02-18 19:57:09
![](https://img.aspxhome.com/file/2023/0/61290_0s.png)
Java毕业设计实战之在线高中考试系统的实现
2021-07-04 06:15:20
![](https://img.aspxhome.com/file/2023/3/71813_0s.jpg)
Unity 制作一个分数统计系统
2021-11-30 03:01:52
![](https://img.aspxhome.com/file/2023/7/106377_0s.gif)
SpringBoot之使用Redis实现分布式锁(秒杀系统)
2022-09-30 15:45:08
![](https://img.aspxhome.com/file/2023/4/82364_0s.png)
Java hashCode原理以及与equals()区别联系详解
2023-03-04 15:37:56
Android中RecyclerView点击Item设置事件
2023-08-22 18:23:19
![](https://img.aspxhome.com/file/2023/1/138821_0s.gif)
android 仿微信demo——注册功能实现(服务端)
2023-10-30 09:58:23
![](https://img.aspxhome.com/file/2023/8/114328_0s.png)
Android 帧动画的实例详解
2023-02-05 16:09:42
JAVA中的Token 基于Token的身份验证实例
2023-11-09 18:05:09
Java获取时间年、月、日的方法
2022-02-10 16:58:36
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
C#如何优雅的结束线程
2022-07-27 19:45:27
聊聊java中引用数据类型有哪些
2022-01-10 11:59:10
如何优雅的处理Spring Boot异常信息详解
2023-11-29 09:50:02
![](https://img.aspxhome.com/file/2023/6/60536_0s.jpg)
Java命名规范
2022-10-03 00:06:59
Mybatis中resultMap的Colum和property属性详解
2023-09-16 11:17:33
![](https://img.aspxhome.com/file/2023/8/58538_0s.png)