当前位置: 首页 > news >正文

Android之图片保存相册及分享图片

文章目录

  • 前言
  • 一、效果图
  • 二、实现步骤
    • 1.引入依赖库
    • 2.二维码生成
    • 3.布局转图片保存或者分享
  • 总结


前言

其实现在很多分享都是我们自定义的,更多的是在界面加了很多东西,然后把整个界面转成图片保存相册和分享,而且现在分享都不需要第三方,直接调用系统分享,大大提高工作效率,本篇文章还涉及到二维码生成,以及布局转图片保存相册并刷新相册功能,

一、效果图

在这里插入图片描述

二、实现步骤

1.引入依赖库

二维码生成依赖库:

 implementation 'com.journeyapps:zxing-android-embedded:3.5.0'

2.二维码生成

//实例化
private var codeBitmap: Bitmap? = null //生成二维码
//share_url 要生成的链接或者文案,第二三个参数为二维码宽高codeBitmap = QRCodeUtils.createQRCodeBitmap(share_url, 120, 120, "UTF-8","H", "1", Color.BLACK, Color.WHITE)
//显示到控件上
imag_ewm.setImageBitmap(codeBitmap)

3.布局转图片保存或者分享

1.调用

//relative_tp为要保存的布局,第二个参数为1时分享,2为保存相册
startSaveBitmap(getViewBitmap(relative_tp), "2")

2.实现方法

  /*** 布局转图片** @param v* @return*/private fun getViewBitmap(v: View): Bitmap? {v.clearFocus()v.isPressed = falseval willNotCache = v.willNotCacheDrawing()v.setWillNotCacheDrawing(false)val color = v.drawingCacheBackgroundColorv.drawingCacheBackgroundColor = 0if (color != 0) {v.destroyDrawingCache()}v.buildDrawingCache()val cacheBitmap = v.drawingCache ?: return nullval bitmap = Bitmap.createBitmap(cacheBitmap)v.destroyDrawingCache()v.setWillNotCacheDrawing(willNotCache)v.drawingCacheBackgroundColor = colorreturn bitmap}/*** 图片保存相册** @param bitmap*/private fun startSaveBitmap(bitmap: Bitmap?, type: String) {//1分享,2为下载if (bitmap == null) {return}// 新建目录appDir,并把图片存到其下val appDir: File = File((this@MyInvite.getExternalFilesDir(null)!!.getPath() + System.currentTimeMillis()).toString() + "BarcodeBitmap")if (!appDir.exists()) {appDir.mkdir()}val fileName = System.currentTimeMillis().toString() + ".jpg"val file = File(appDir, fileName)try {val fos = FileOutputStream(file)bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)fos.flush()fos.close()} catch (e: IOException) {e.printStackTrace()}if (type == "1") {val intent = Intent(Intent.ACTION_SEND)intent.type = "image/*" //设置MIME类型intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this,"applicationId(也就是包名).fileprovider",file)) //需要分享的文件URIstartActivity(Intent.createChooser(intent, "分享"))} else {//把file里面的图片插入到系统相册中try {MediaStore.Images.Media.insertImage(this@MyInvite.getContentResolver(),file.absolutePath, fileName, null)} catch (e: FileNotFoundException) {e.printStackTrace()}// 通知相册更新this@MyInvite.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(file)))ToastUtils.showToast(resources.getString(R.string.Successfullysaved))}}

总结

总之这玩意简单如喝水,欢迎大家提建议,但我不会采纳,希望能帮助到有需要的。


http://www.mrgr.cn/news/92245.html

相关文章:

  • blender bpy渲染禁用日志
  • 【前端基础】Day 1 HTML
  • 6层高速PCB设计入门第1~10讲
  • 首次使用WordPress建站的经验分享(一)
  • SQL笔记#函数、谓词、CASE表达式
  • 运行测试用例
  • Orange 开源项目 - 集成阿里云大模型
  • Redis速成(1)
  • 【Python LeetCode 专题】位运算
  • 图论算法篇:BFS宽度优先遍历
  • 【数据结构】链表中快指针和慢指针
  • Zap:Go 的高性能日志库
  • Ollama部署本地大模型DeepSeek-R1-Distill-Llama-70B
  • JavaWeb开发入门:从前端到后端的完整流程解析
  • Fetch API 与 XMLHttpRequest:深入剖析异步请求的利器
  • BUU40 [CSCCTF 2019 Qual]FlaskLight1【SSTI】
  • **模式的好处 (设计模式)
  • Linux第三讲----用户权限(二)
  • 【第五节】C++设计模式(创建型模式)-Prototype(原型)模式
  • Three.js 快速入门教程【六】相机控件 OrbitControls