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

Android 保存图片

这个主要讲的InputStream去保存。
如果需要BItmap与InputStream相互转换可以参考

Android Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系

保存图片我们需要考虑系统版本,Q前后还是不一样的。

  /*** 保存图片* @param context  上下文* @param inputStream 流* @param suffixName  后缀名(.png  .jpg)* @return* @throws IOException*/public static String saveImageFromInput(Context context, InputStream inputStream, String suffixName) throws IOException {if (androidQ()) {Uri uri = saveImageFromInputByAndroidQMedia(context, inputStream, suffixName);String imagePathByUri = getImagePathByUri(context, uri);return imagePathByUri;} else {String imagePtah = createFilePath(context, suffixName,randomName() + suffixName).getAbsolutePath();saveImageFromInputByAndroid(context, inputStream, imagePtah);return imagePtah;}}/*** 保存媒体库(AndroidQ)* @param context* @param inputStream* @param suffixName* @return*/private static Uri saveImageFromInputByAndroidQMedia(Context context, InputStream inputStream, String suffixName) {BufferedInputStream bis = null;OutputStream outputStream = null;BufferedOutputStream bos = null;Uri uri = null;try {bis = new BufferedInputStream(inputStream);uri = getImageUriByMediaStore(context, suffixName);if (uri != null) {outputStream = context.getContentResolver().openOutputStream(uri);if (outputStream != null) {bos = new BufferedOutputStream(outputStream);byte[] buf = new byte[102400];int bytes = bis.read(buf);while (bytes >= 0) {bos.write(buf, 0, bytes);bos.flush();bytes = bis.read(buf);}}}} catch (IOException e) {e.printStackTrace();} finally {closeSilently(bis);closeSilently(bos);closeSilently(outputStream);return uri;}}/*** 保存本地* @param context* @param inputStream* @param imagePath*/private static void saveImageFromInputByAndroid(Context context, InputStream inputStream, String imagePath) {BufferedInputStream bis = null;OutputStream outputStream = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(inputStream);outputStream = new FileOutputStream(imagePath);if (outputStream != null) {bos = new BufferedOutputStream(outputStream);byte[] buf = new byte[102400];int bytes = bis.read(buf);while (bytes >= 0) {bos.write(buf, 0, bytes);bos.flush();bytes = bis.read(buf);}}} catch (IOException e) {e.printStackTrace();} finally {closeSilently(bis);closeSilently(bos);closeSilently(outputStream);}}public static Uri getImageUriByMediaStore(Context context, String suffixName) throws IOException {Uri uri;if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {// 7.0之前获取uri方式uri = Uri.fromFile(createFilePath(context, "image", randomName() + suffixName));} else if (androidQ()) {ContentValues contentValues = new ContentValues();contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, randomName() + suffixName);contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/*");contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);} else {//7.0之后获取uri方式uri = FileProvider.getUriForFile(context, FileAppProvider.getProviderName(context), createFilePath(context, "image", randomName() + suffixName));}return uri;}/*** 创建文件路径* @param context* @param folderName* @param fileName* @return* @throws IOException*/private static File createFilePath(Context context, String folderName, String fileName) throws IOException {String path;if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/app/" + folderName + "/" + fileName;} else {path = context.getCacheDir().getAbsolutePath() + "/app/" + folderName + "/" + fileName;}return createFile(new File(path));}/*** 创建文件* @param imageFile* @return* @throws IOException*/private static File createFile(File imageFile) throws IOException {if (!imageFile.getParentFile().exists()) {imageFile.getParentFile().mkdirs();}if (imageFile.exists()) {imageFile.delete();}if (!imageFile.exists()) {imageFile.createNewFile();}return imageFile;}/*** 获取uri 对应 path* @param context* @param uri* @return*/public static String getImagePathByUri(Context context, Uri uri) {String imagePath = null;if (context != null && uri != null) {String[] proj = {MediaStore.Images.Media.DATA};Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);if (cursor.moveToFirst()) {int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);imagePath = cursor.getString(column_index);}cursor.close();}return imagePath;}public static void closeSilently(Closeable c) {if (c == null) return;try {c.close();} catch (Throwable t) {// Do nothing}}public static boolean androidQ() {return Build.VERSION.SDK_INT > Build.VERSION_CODES.Q;}/*** 生成随机名称** @return*/public static String randomName() {return System.currentTimeMillis() + "_" + new Random().nextInt();}

FileAppProvider
public class FileAppProvider extends FileProvider {public static String getProviderName(Context context) {return context.getPackageName() + ".app.file.app.provider";}
}

file_app_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Copyright 2017 Yan Zhenjie.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<paths><external-pathname="external_path"path="."/><external-files-pathname="external_files_path"path="."/><external-cache-pathname="external_cache_path"path="."/><files-pathname="file_path"path="."/><cache-pathname="cache_path"path="."/></paths>

清单文件添加:

  <providerandroid:name="com.app.file.provider.FileZqcfProvider"android:authorities="${applicationId}.app.file.app.provider"android:exported="false"android:grantUriPermissions="true"android:multiprocess="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_app_provider" /></provider>

相关文章:

Android 保存图片

这个主要讲的InputStream去保存。 如果需要BItmap与InputStream相互转换可以参考 Android Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系 保存图片我们需要考虑系统版本&#xff0c;Q前后还是不一样的。 /*** 保存图片* param context 上下文* param inputS…...

Android相机-架构

引言&#xff1a; 主要是针对CameraAPI v2 HAL3的架构对Android相机系统进行梳理。 相机架构 App和FrameWork Camera API v2位于&#xff1a; packages/apps/Camer2 frameworks/ex/camera2 应用框架级别&#xff0c;使用Camera2 API与相机的硬件进行交互。通过调用Binder接口…...

从C语言到C++_33(C++11_上)initializer_list+右值引用+完美转发+移动构造/赋值

目录 1. 列表初始化initializer_list 2. 前面提到的一些知识点 2.1 小语法 2.2 STL中的一些变化 3. 右值和右值引用 3.1 右值和右值引用概念 3.2 右值引用类型的左值属性 3.3 左值引用与右值引用比较 3.4 右值引用的使用场景 3.4.1 左值引用的功能和短板 3.4.2 移动…...

如何在Linux系统中处理PDF文件?

如何在Linux系统中处理PDF文件&#xff1f; 1.查看PDF文档2.合并PDF文档3.压缩PDF文档4.提取PDF文本 PDF文件是一种特殊的文件格式&#xff0c;它可以在不同的操作系统中实现跨平台的文件传输和共享。Linux系统作为一种自由开放的操作系统&#xff0c;拥有丰富的PDF文件处理工具…...

SpringBoot实现热部署/加载

在我们修改完项目代码后希望不用重启服务器就能把项目代码部署到服务器中(也就是说修改完项目代码后不用重启服务器修改后的项目代码就能生效)。 一、实现devtools原理 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-…...

我是如何使用Spring Retry减少1000 行代码

使用 Spring Retry 重构代码的综合指南。 问题介绍 在我的日常工作中&#xff0c;我主要负责开发一个庞大的金融应用程序。当客户发送请求时&#xff0c;我们使用他们的用户 ID 从第三方服务获取他们的帐户信息&#xff0c;保存交易并更新缓存中的详细信息。尽管整个流程看起来…...

ARM开发(stm32 cortex-A7核IIC实验)

1.实验目标&#xff1a;采集温湿度传感器值&#xff1b; 2.分析框图&#xff08;模拟IIC控制器&#xff09;&#xff1b; 3.代码&#xff1b; ---iic.h封装时序协议头文件--- #ifndef __IIC_H__ #define __IIC_H__ #include "stm32mp1xx_gpio.h" #include "st…...

「Java」《Java集合框架详解:掌握常用集合类,提升开发效率》

Java集合框架详解&#xff1a;掌握常用集合类&#xff0c;提升开发效率 摘要&#xff1a;一. 引言二. 集合框架概述三. 集合接口详解四. 集合类的选择五. 泛型和类型安全六. 集合的线程安全七. 高级集合类和算法八、Java集合实践操作示例1. 创建和初始化集合&#xff1a;2. 遍历…...

游戏出海需知:Admob游戏广告变现策略

越来越多的出海游戏公司更加重视应用内的广告变现&#xff0c;而 AdMob因为其提供的丰富的广告资源&#xff0c;稳定平台支持&#xff0c;被广泛接入采用。 Admob推出的广告变现策略包括bidding、插页式激励视频、开屏广告、各种细分功能的报告等等。 一、Bidding 竞价策略 …...

【linux】NFS调试总结

文章目录 00. ENV10. 简述20. 下载、安装、配置30. 使用1. 从uboot中设置NFS启动文件系统2. 调试 80. 问题1. NFS版本不匹配问题 90. 附件91. 服务端NFS配置项简述 00. ENV ubuntn1804 10. 简述 百度百科&#xff1a;https://baike.baidu.com/item/%E7%BD%91%E7%BB%9C%E6%96%87…...

wireshark进行网络监听

一、实验目的&#xff1a; 1&#xff09;掌握使用CCProxy配置代理服务器&#xff1b; 2&#xff09;掌握使用wireshark抓取数据包&#xff1b; 3&#xff09;能够对数据包进行简单的分析。 二、预备知识&#xff1a; 包括监听模式、代理服务器、中间人攻击等知识点&#xf…...

时间复杂度

一、时间复杂度 时间复杂度是计算机科学中用来衡量算法运行时间随输入规模增加而增长的速度。简单来说&#xff0c;它是一个衡量算法执行效率的指标&#xff0c;表示算法运行所需时间与输入数据量之间的关系。 时间复杂度通常用大O符号&#xff08;O&#xff09;来表示&#…...

Unity实现广告滚动播放、循环播放、鼠标切换的效果

效果&#xff1a; 场景结构&#xff1a; 特殊物体&#xff1a;panel下面用排列组件horizent layout group放置多个需要显示的面板&#xff0c;用mask遮罩好。 using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; using Unity…...

LangChain + Streamlit + Llama:将对话式AI引入本地机器

推荐&#xff1a;使用 NSDT场景编辑器 助你快速搭建可二次编辑的3D应用场景 什么是LLMS&#xff1f; 大型语言模型 &#xff08;LLM&#xff09; 是指能够生成与人类语言非常相似的文本并以自然方式理解提示的机器学习模型。这些模型使用包括书籍、文章、网站和其他来源在内的…...

Python 读写 Excel 文件库推荐和使用教程

文章目录 前言Python 读写 Excel 库简介openpyxl 处理 Excel 文件教程pandas 处理 Excel 文件教程总结 前言 Python 读写 Excel 文件的库总体看还是很多的&#xff0c; 各有其优缺点&#xff0c; 以下用一图总结各库的优缺点&#xff0c; 同时对整体友好的库重点介绍其使用教程…...

“深入解析JVM:理解Java虚拟机的工作原理和优化技巧“

标题&#xff1a;深入解析JVM&#xff1a;理解Java虚拟机的工作原理和优化技巧 摘要&#xff1a;本文将深入探讨Java虚拟机&#xff08;JVM&#xff09;的工作原理和优化技巧。我们将从JVM的基本结构开始&#xff0c;逐步介绍其工作原理&#xff0c;并提供一些实际示例代码&am…...

解决SEGGER Embedded Studio无法显示Nordic MCU外设寄存器问题

如果使用SES调试NRF52840的时候发现&#xff0c;官方例程只能显示CPU寄存器&#xff0c;但是无法显示外设寄存器时&#xff0c;解决办法如下&#xff1a; 1.在解决方案右键→Options→Debug→Debugger&#xff0c;然后Target Device选择正确的型号。 2.Register Definition Fil…...

Oracle-day1:scott用户、查询、取整、截取、模糊查询、别名——23/8/23

整理一下第一天软件测试培训的知识点 1、scott用户 -- 以system管理员登录锁定scott用户 alter user scott account lock;-- 以system管理员登录解锁scott用户 alter user scott account unlock;-- 以system管理员用户设置scott用户密码 alter user scott identfied by tiger…...

stm32之3.key开关

假设key电阻为40kΩ&#xff0c;则key0 的电压3.3v*4/52.64v 2.key开关代码 ② GPIO_OType_PP//推挽输出 GPIO_OType_PP//开漏输出 推挽输出是指输出端口可以同时提供高电平和低电平输出&#xff0c;而开漏输出则是指输出端口只能提供低电平输出&#xff0c;高电平时需要借…...

GPT带我学-设计模式-代理模式

什么是代理模式 代理模式&#xff08;Proxy Pattern&#xff09;是设计模式中的一种结构型模式&#xff0c;它为其他对象提供一种代理以控制对这个对象的访问。 代理模式有三个主要角色&#xff1a;抽象主题&#xff08;Subject&#xff09;、真实主题&#xff08;Real Subje…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现

摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序&#xff0c;以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务&#xff0c;提供稳定高效的数据处理与业务逻辑支持&#xff1b;利用 uniapp 实现跨平台前…...

CMake 从 GitHub 下载第三方库并使用

有时我们希望直接使用 GitHub 上的开源库,而不想手动下载、编译和安装。 可以利用 CMake 提供的 FetchContent 模块来实现自动下载、构建和链接第三方库。 FetchContent 命令官方文档✅ 示例代码 我们将以 fmt 这个流行的格式化库为例,演示如何: 使用 FetchContent 从 GitH…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

Pinocchio 库详解及其在足式机器人上的应用

Pinocchio 库详解及其在足式机器人上的应用 Pinocchio (Pinocchio is not only a nose) 是一个开源的 C 库&#xff0c;专门用于快速计算机器人模型的正向运动学、逆向运动学、雅可比矩阵、动力学和动力学导数。它主要关注效率和准确性&#xff0c;并提供了一个通用的框架&…...

【从零学习JVM|第三篇】类的生命周期(高频面试题)

前言&#xff1a; 在Java编程中&#xff0c;类的生命周期是指类从被加载到内存中开始&#xff0c;到被卸载出内存为止的整个过程。了解类的生命周期对于理解Java程序的运行机制以及性能优化非常重要。本文会深入探寻类的生命周期&#xff0c;让读者对此有深刻印象。 目录 ​…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

如何应对敏捷转型中的团队阻力

应对敏捷转型中的团队阻力需要明确沟通敏捷转型目的、提升团队参与感、提供充分的培训与支持、逐步推进敏捷实践、建立清晰的奖励和反馈机制。其中&#xff0c;明确沟通敏捷转型目的尤为关键&#xff0c;团队成员只有清晰理解转型背后的原因和利益&#xff0c;才能降低对变化的…...