Android 保存本地图片

server/2024/9/25 8:34:24/

1.工具类BitmapUtils

public class BitmapUtils {public static Bitmap mirror(Bitmap rawBitmap) {Matrix matrix = new Matrix();matrix.postScale(-1f, 1f);return Bitmap.createBitmap(rawBitmap, 0, 0, rawBitmap.getWidth(), rawBitmap.getHeight(), matrix, true);}public static Bitmap rotateBitmap(Bitmap bm, int degree) {Bitmap returnBm = null;// 根据旋转角度,生成旋转矩阵Matrix matrix = new Matrix();matrix.postRotate(degree);try {// 将原始图片按照旋转矩阵进行旋转,并得到新的图片returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);} catch (OutOfMemoryError e) {}if (returnBm == null) {returnBm = bm;}if (bm != returnBm) {bm.recycle();}return returnBm;}/*** 将bitmap转换成bytes*/public static byte[] bitmapToBytes(Bitmap bitmap, int quality) {if (bitmap == null) {return null;}int size = bitmap.getWidth() * bitmap.getHeight() * 4;ByteArrayOutputStream out = new ByteArrayOutputStream(size);try {bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);out.flush();out.close();return out.toByteArray();} catch (IOException e) {return null;}}/*** 将图片保存到磁盘中** @param bitmap* @param file   图片保存目录——不包含图片名* @param path   图片保存文件路径——包含图片名* @return*/public static boolean saveBitmap(Bitmap bitmap, File file, File path) {boolean success = false;byte[] bytes = bitmapToBytes(bitmap, 100);OutputStream out = null;try {if (!file.exists() && file.isDirectory()) {file.mkdirs();}out = new FileOutputStream(path);out.write(bytes);out.flush();success = true;} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}return success;}/*** 高级图片质量压缩** @param bitmap 位图* @param width  压缩后的宽度,单位像素*/public static Bitmap imageZoom(Bitmap bitmap, double width) {// 将bitmap放至数组中,意在获得bitmap的大小(与实际读取的原文件要大)ByteArrayOutputStream baos = new ByteArrayOutputStream();// 格式、质量、输出流bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);byte[] b = baos.toByteArray();Bitmap newBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);// 获取bitmap大小 是允许最大大小的多少倍return scaleWithWH(newBitmap, width,width * newBitmap.getHeight() / newBitmap.getWidth());}/**** 图片缩放*@param bitmap 位图* @param w 新的宽度* @param h 新的高度* @return Bitmap*/public static Bitmap scaleWithWH(Bitmap bitmap, double w, double h) {if (w == 0 || h == 0 || bitmap == null) {return bitmap;} else {int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidth = (float) (w / width);float scaleHeight = (float) (h / height);matrix.postScale(scaleWidth, scaleHeight);return Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix, true);}}/*** bitmap保存到指定路径** @param file 图片的绝对路径* @param file 位图* @return bitmap*/public static boolean saveFile(String file, Bitmap bmp) {if (TextUtils.isEmpty(file) || bmp == null) return false;File f = new File(file);if (f.exists()) {f.delete();} else {File p = f.getParentFile();if (!p.exists()) {p.mkdirs();}}try {FileOutputStream out = new FileOutputStream(f);bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);out.flush();out.close();} catch (IOException e) {e.printStackTrace();return false;}return true;}/*** 保存bitmap到SD卡,请确认应用有存储权限** @param bmp     获取的bitmap数据* @param picName 自定义的图片名*/public static File saveBmp2Gallery(Context context, String fileSavePath, Bitmap bmp, String picName) {File galleryPath = new File(fileSavePath);if (!galleryPath.exists()) {galleryPath.mkdir();}// 声明文件对象File file = null;// 声明输出流FileOutputStream outStream = null;String fileName = null;try {// 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件file = new File(galleryPath, picName + ".jpg");
//            if(file.exists()){
//                file.delete();
//                file = new File(galleryPath, picName + ".jpg");
//            }
//            file = new File(galleryPath, photoName);// 获得文件相对路径fileName = file.toString();// 获得输出流,如果文件中有内容,追加内容outStream = new FileOutputStream(fileName);if (null != outStream) {bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);}} catch (Exception e) {e.getStackTrace();} finally {try {if (outStream != null) {outStream.flush();outStream.close();}} catch (IOException e) {e.printStackTrace();}}try {MediaScannerConnection.scanFile(context, new String[]{fileName}, null, null);Toast.makeText(context, context.getResources().getString(R.string.pic_save_success), Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(context, context.getResources().getString(R.string.pic_save_fail), Toast.LENGTH_SHORT).show();}return file;}public static boolean saveBmp2Gallery2(Context context, String fileSavePath, Bitmap bmp, String picName) {isFolderExists(fileSavePath);File galleryPath = new File(fileSavePath);if (!galleryPath.exists()) {galleryPath.mkdir();}// 声明文件对象File file = null;// 声明输出流FileOutputStream outStream = null;String fileName = null;try {// 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件file = new File(galleryPath, picName + ".jpg");
//            if(file.exists()){
//                file.delete();
//                file = new File(galleryPath, picName + ".jpg");
//            }
//            file = new File(galleryPath, photoName);// 获得文件相对路径fileName = file.toString();// 获得输出流,如果文件中有内容,追加内容outStream = new FileOutputStream(fileName);if (null != outStream) {bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);}} catch (Exception e) {e.getStackTrace();} finally {try {if (outStream != null) {outStream.flush();outStream.close();}} catch (IOException e) {e.printStackTrace();}}try {MediaScannerConnection.scanFile(context, new String[]{fileName}, null, null);Toast.makeText(context, context.getResources().getString(R.string.pic_save_success), Toast.LENGTH_SHORT).show();return true;} catch (Exception e) {e.printStackTrace();Toast.makeText(context, context.getResources().getString(R.string.pic_save_fail), Toast.LENGTH_SHORT).show();}return false;}//判断是否存在,不存在创建文件夹public static boolean isFolderExists(String strFolder) {File file = new File(strFolder);if (!file.exists()) {if (file.mkdirs()) {return true;} else {return false;}}return true;}/*** 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准** @param backBitmap  在底部的位图* @param frontBitmap 盖在上面的位图* @return*/public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap frontBitmap, int leftFront, int topFront) {if (backBitmap == null || backBitmap.isRecycled()|| frontBitmap == null || frontBitmap.isRecycled()) {return null;}//        Bitmap bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true);
//        Canvas canvas = new Canvas(bitmap);
//        canvas.drawBitmap(backBitmap, 0, 0, null);
//        canvas.drawBitmap(frontBitmap, leftFront, topFront, null);Bitmap bitmap = frontBitmap.copy(Bitmap.Config.ARGB_8888, true);Canvas canvas = new Canvas(bitmap);float x = (float)(bitmap.getWidth()) /   (float)(backBitmap.getWidth());float y =  (float)(bitmap.getHeight()) /   (float)(backBitmap.getHeight());Matrix matrix = new Matrix();matrix.postScale(x, y); // 缩放比例  大于1为放大backBitmap = Bitmap.createBitmap(backBitmap, 0, 0, backBitmap.getWidth(), backBitmap.getHeight(), matrix, true);canvas.drawBitmap(backBitmap, 0, 0, null);canvas.drawBitmap(frontBitmap, 0, 0, null);return bitmap;}/*** 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准** @param bytes  在底部的位图* @param bytes2 盖在上面的位图*/public static void savaRawFile(byte[] bytes, byte[] bytes2) {try {File path = new File("/sdcard");if (!path.exists() && path.isDirectory()) {path.mkdirs();}File file = new File("/sdcard/", new SimpleDateFormat("_HHmmss_yyMMdd").format(new Date(System.currentTimeMillis())) + ".bin");FileOutputStream fos = new FileOutputStream(file);fos.write(bytes);fos.write(bytes2);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 保存红外数据** @param bytes*/public static void savaIRFile(byte[] bytes) {try {File path = new File("/sdcard");if (!path.exists() && path.isDirectory()) {path.mkdirs();}File file = new File("/sdcard/", "ir" + new SimpleDateFormat("_HHmmss_yyMMdd").format(new Date(System.currentTimeMillis())) + ".bin");FileOutputStream fos = new FileOutputStream(file);fos.write(bytes);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 保存温度数据** @param bytes*/public static void savaTempFile(byte[] bytes) {try {File path = new File("/sdcard");if (!path.exists() && path.isDirectory()) {path.mkdirs();}File file = new File("/sdcard/", "temp" + new SimpleDateFormat("_HHmmss_yyMMdd").format(new Date(System.currentTimeMillis())) + ".bin");FileOutputStream fos = new FileOutputStream(file);fos.write(bytes);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** @param context* @param file* @return*/public static boolean isFileExists(Context context, final File file) {if (file == null) return false;if (file.exists()) {return true;}return isFileExists(context, file.getAbsolutePath());}/*** Return whether the file exists.** @param filePath The path of file.* @return {@code true}: yes<br>{@code false}: no*/public static boolean isFileExists(Context context, final String filePath) {File file = new File(filePath);if (file == null) return false;if (file.exists()) {return true;}return isFileExistsApi29(context, filePath);}/*** @param context* @param filePath* @return*/private static boolean isFileExistsApi29(Context context, String filePath) {if (Build.VERSION.SDK_INT >= 29) {try {Uri uri = Uri.parse(filePath);ContentResolver cr = context.getContentResolver();AssetFileDescriptor afd = cr.openAssetFileDescriptor(uri, "r");if (afd == null) return false;try {afd.close();} catch (IOException ignore) {}} catch (FileNotFoundException e) {return false;}return true;}return false;}/*** short数组转byte数组** @param src* @return*/private static byte[] toByteArray(short[] src) {int count = src.length;byte[] dest = new byte[count << 1];for (int i = 0; i < count; i++) {dest[i * 2] = (byte) ((src[i] >> 8) & 0xFF);dest[i * 2 + 1] = (byte) (src[i] & 0xFF);}return dest;}/*** byte数组转short数组** @param src* @return*/public static short[] toShortArray(byte[] src) {int count = src.length >> 1;short[] dest = new short[count];for (int i = 0; i < count; i++) {dest[i] = (short) ((src[i * 2] & 0xFF) << 8 | ((src[2 * i + 1] & 0xFF)));}return dest;}/*** @param bytes* @param fileTitle*/public static void saveShortFile(String fileDir, short[] bytes, String fileTitle) {// 创建目录createOrExistsDir(fileDir);try {File file = new File(fileDir, fileTitle + ".bin");createOrExistsDir(file);Log.i("TAG", "getAbsolutePath = " + file.getAbsolutePath());FileOutputStream fos = new FileOutputStream(file);fos.write(toByteArray(bytes));fos.close();} catch (IOException e) {e.printStackTrace();}}/*** @param file*/private static void createOrExistsDir(File file) {// 文件不存在则创建文件if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}}/*** 如果文件夹不存在则创建** @param fileDir*/private static void createOrExistsDir(String fileDir) {File file = new File(fileDir);//如果文件夹不存在则创建if (!file.exists() && !file.isDirectory()) {//不存在file.mkdir();} else {//目录存在}}private static int sBufferSize = 524288;/*** @param context* @param file* @return*/public static byte[] readFile2BytesByStream(Context context, final File file) {if (!isFileExists(context, file)) return null;try {ByteArrayOutputStream os = null;InputStream is = new BufferedInputStream(new FileInputStream(file), sBufferSize);try {os = new ByteArrayOutputStream();byte[] b = new byte[sBufferSize];int len;while ((len = is.read(b, 0, sBufferSize)) != -1) {os.write(b, 0, len);}return os.toByteArray();} catch (IOException e) {e.printStackTrace();return null;} finally {try {is.close();} catch (IOException e) {e.printStackTrace();}try {if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}}} catch (FileNotFoundException e) {e.printStackTrace();return null;}}}

2.使用方法
 

// 保存图片boolean isSucc = BitmapUtils.saveBmp2Gallery2(this, App.getInstance().INFISENSE_SAVE_DIR + File.separator + DateUtils.getCurrentTime_Today("yyyy-MM-dd"), bitmap, "名称");

3.保存地址


/***图片保存的文件夹*/public String INFISENSE_SAVE_DIR;@Overridepublic void onCreate() {super.onCreate();INFISENSE_SAVE_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+ File.separator + Constant.FOLDER_INFRARED_TEMP;//FOLDER_INFRARED_TEMP文件夹名称File file = new File(INFISENSE_SAVE_DIR);if (!file.exists()){file.mkdirs();}}


http://www.ppmy.cn/server/121730.html

相关文章

设计原则模式概览

前言 架构设计是软件系统稳定的核心因素&#xff0c;也是程序员晋级架构师的核心因素&#xff0c;建议日常开发过程中针对设计进行深挖与思考 核心 分清楚哪些是稳定的&#xff0c;哪些是变化的&#xff08;一定有稳定跟变化的成分&#xff09;&#xff1b; 捋清楚哪些是类设计…

智能听诊器宠物社区的新宠

在宠物社区中&#xff0c;智能听诊器正迅速成为宠物主人的新宠。这款设备通过高精度传感器和智能算法&#xff0c;为宠物提供实时的健康监测和诊断建议。智能听诊器的移动健康应用&#xff0c;使得宠物主人能够随时随地访问宠物的健康数据&#xff0c;并与兽医进行交流。 智能…

python学习-11【图形用户界面】

1、EasyGUI 快速入门 在命令提示符中使用 pip install easygui 命令下载并安装 Easy GUI 功能演示 1、 msgbox() 函数可以显示一个对话框&#xff0c;默认提供名为 OK 的按钮 msgbox(msg(message), title, ok_buttonOK, imageNone, rootNone) 2、ccbox() 函数用于提供选择功能…

Cilium + ebpf 系列文章- XDP (eXpress data Path)(四)

前言: 现有网络容器的性能消耗与通信简单流程: 1、只要是Pod都有自己的网络命名空间。 2、Pod的网络命名空间由Pod内的容器使用并共享但是由暂停容器(Pause Container)管理。 3、Pod内的容器网络通过veth_pair对与主机网络命名空间打通。 …

短视频矩阵管理系统贴牌 源码开发

抖音账号矩阵的开发核心维度包括&#xff1a; 多账号管理开发维度&#xff1a;通过运用不同类型的账号矩阵&#xff0c;可以实现统一且便捷的管理。目前&#xff0c;矩阵系统支持管理抖音、快手、视频号,b站的账号&#xff0c;未来计划加入小红书,tk等等的账号管理。 矩阵账号…

蓝队技能-应急响应篇Web内存马查杀JVM分析Class提取诊断反编译日志定性

知识点&#xff1a; 1、应急响应-Web内存马-定性&排查 2、应急响应-Web内存马-分析&日志 注&#xff1a;传统WEB类型的内存马只要网站重启后就清除了。 演示案例-蓝队技能-JAVA Web内存马-JVM分析&日志URL&内存查杀 0、环境搭建 参考地址&#xff1a;http…

如何安装部署kafka

安装和部署Apache Kafka需要以下几个步骤&#xff0c;包括下载 Kafka、配置 ZooKeeper&#xff08;或者使用 Kafka 自带的 Kafka Raft 模式替代 ZooKeeper&#xff09;&#xff0c;以及启动 Kafka 服务。以下是一个但基于 Linux 的典型安装流程&#xff0c;可以根据需要改装到其…

高级算法设计与分析 学习笔记7 数据结构扩充

OS tree 数据统计树 本体是红黑树&#xff0c;除了自己的数字外还要记录自己这颗子树有几个节点。 这种特性可以让使用者迅速找到自己要找的第i个数。 往左走&#xff0c;给的那个数字就是排名&#xff0c;但是往右边走的话&#xff0c;那就的先加上当前的排名&#xff08;也就…