【代码】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)
    }
}
相关推荐
yueqc14 小时前
Kotlin 协程 Flow 操作符总结
kotlin·协程·flow
fanged6 小时前
天马G前端的使用
android·游戏
molong93110 小时前
Kotlin 内联函数、高阶函数、扩展函数
android·开发语言·kotlin
叶辞树11 小时前
Android framework调试和AMS等服务调试
android
慕伏白13 小时前
【慕伏白】Android Studio 无线调试配置
android·ide·android studio
低调小一14 小时前
Kuikly 小白拆解系列 · 第1篇|两棵树直调(Kotlin 构建与原生承载)
android·开发语言·kotlin
跟着珅聪学java14 小时前
spring boot 整合 activiti 教程
android·java·spring
川石课堂软件测试15 小时前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
2501_9159214316 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9159214316 小时前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview