【代码】JetpackComposeKotlin分享Bitmap图片

目录

  • [1. 发送函数](#1. 发送函数)
  • [2. 配置fileProvider](#2. 配置fileProvider)
  • [3. bitmap保存本地的函数](#3. bitmap保存本地的函数)
  • [4. 全局变量](#4. 全局变量)
  • [5. 封装的ButtonIcon函数](#5. 封装的ButtonIcon函数)

Android12在jetpack compose中使用Kotlin代码分享Bitmap图片。

1. 发送函数

kotlin 复制代码
@Composable
fun Share() {
    val context = LocalContext.current
    ButtonIcon(icon = Icons.Rounded.Share) {
        val file = File(lastSavePath)
        if(!file.exists()){
            if(!saveBitmapTemp(context)){
                return@ButtonIcon
            }
        }
        val shareIntent = Intent(Intent.ACTION_SEND)
        val newFile = File(lastSavePath)
        val uri = getUriForFile(context, "com.jeady.composeCamerax.fileprovider", newFile)
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
        shareIntent.setType("image/*")
        startActivity(context, Intent.createChooser(shareIntent, "发给某人"), null)
    }
}

2. 配置fileProvider

AndroidMinifest.xml

xml 复制代码
    <application>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.jeady.composeCamerax.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider" />
        </provider>

file_provider.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="root" path="" />
    <external-files-path name="dcim_file" path="DCIM"/>
    <external-cache-path name="cache_file" path="."/>
</paths>

3. bitmap保存本地的函数

kotlin 复制代码
fun saveBitmapTemp(context: Context):Boolean {
    lastSavePath = (context.externalCacheDir?.path ?: context.cacheDir.path) + "/cameraxx_" + Date().time + ".jpg"
    val fo = FileOutputStream(lastSavePath)
    return if(currentBitmap?.compress(Bitmap.CompressFormat.JPEG, 50, fo)==true){
        Toast.makeText(context, "保存缓存文件", Toast.LENGTH_SHORT).show()
        true
    }else{
        Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show()
        false
    }
}

4. 全局变量

kotlin 复制代码
var currentBitmap by mutableStateOf<Bitmap?>(null)
var lastSavePath = ""

5. 封装的ButtonIcon函数

kotlin 复制代码
@Composable
fun ButtonIcon(icon: ImageVector, onclick: ()->Unit) {
    IconButton(onClick = onclick) {
        Icon(icon, "", tint = Color.Gray)
    }
}
相关推荐
fatiaozhang95271 小时前
晶晨S905L3A(B)-安卓9.0-开启ADB和ROOT-支持IPTV6-支持外置游戏系统-支持多种无线芯片-支持救砖-完美通刷线刷固件包
android·游戏·adb·华为·电视盒子·机顶盒rom·魔百盒固件
行墨2 小时前
Kotlin语言的==与===比较操作
android
圣火喵喵教2 小时前
Pixel 8 pro 刷AOSP源码 Debug 详细教程(含救砖)
android
二流小码农3 小时前
鸿蒙开发:使用Ellipse绘制椭圆
android·ios·harmonyos
自不量力的A同学3 小时前
谷歌将 Android OS 完全转变为 “内部开发”
android
行墨3 小时前
Kotlin 的可空类型
android
suren3 小时前
deepseek ai 输入法
android
tangweiguo030519874 小时前
Android并发编程:线程池与协程的核心区别与最佳实践指南
android·kotlin
二流小码农4 小时前
鸿蒙开发:使用Circle绘制圆形
android·ios·harmonyos
行墨4 小时前
Kotlin内置函数之takeIf 和 takeUnless
android