【代码】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)
    }
}
相关推荐
-SOLO-11 分钟前
解决VMware 显示比例被重置的问题
android
alexhilton1 小时前
探究Android Views、Flutter和Compose如何渲染你的UI
android·kotlin·android jetpack
Lesile4 小时前
Android:Hilt框架入门 · 在ViewUI和ComposeUI下的应用
android·android jetpack
用户423816229074 小时前
Android 16 WebView 页面顶部挖孔/通知栏不能显示UI问题排查
android
weixin_727535625 小时前
Loop 已死,Graph 新生:AI 工作流的范式革命
android·人工智能·rxjava
严同学正在努力6 小时前
从备份到恢复:我用 30 分钟恢复了误删的核心业务表
android·java·数据库·ai
a1117766 小时前
中文优先的企业 RAG 知识库 开源项目
开发语言·开源·kotlin
梦想三三6 小时前
LangChain Output Parser 实战:从字符串到结构化数据的完整指南
android·服务器·langchain·github·uv
爱笑鱼8 小时前
Binder(八):远端进程死了,BinderProxy 为什么还能收到通知?
android
Android-Flutter8 小时前
Kotlin 冷流与热流详解
android·kotlin