【代码】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)
    }
}
相关推荐
雨白2 小时前
实现双向滑动的 ScalableImageView(上)
android
木头左2 小时前
结合机器学习的Backtrader跨市场交易策略研究
人工智能·机器学习·kotlin
Y4090012 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
没有了遇见7 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_916008898 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921438 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO9 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO9 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao11 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析
翻滚丷大头鱼11 小时前
android 四大组件—BroadcastReceiver
android