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

Android 保存本地图片

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;

@Override
    public 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.kler.cn/news/323549.html

相关文章:

  • 深度学习(入门)03:监督学习
  • 9.24 C++ 常成员,运算符重载
  • 人工智能-机器学习-深度学习-分类与算法梳理
  • qt 模仿简易的软狗实现
  • Java NIO 全面详解:掌握 `Path` 和 `Files` 的一切
  • Keysight 下载信源 Visa 指令
  • 蓝桥杯模块二:数码管的静态、动态实现
  • 电脑录屏怎么录视频和声音?苹果macOS、windows10都可以用的原神录屏工具来啦
  • 【JAVA】算法笔记
  • Linux用户管理
  • 面试遇到的质量体系10个问题(深度思考)
  • 论文阅读 | 可证安全隐写(网络空间安全科学学报 2023)
  • 神经网络(二):卷积神经网络
  • Vscode 远程切换Python虚拟环境
  • 解决Android中使用jdk 9以上中的某个类(AbstractProcessor)但是无法导入的问题
  • Java中通过方法上注解实现入参校验
  • 计算机毕业设计 在线问诊系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • C++那些你不得不知道的(2)
  • .NET 控制台应用程序连接 MySQL 数据库实现增删改查
  • mysql数据库设置主从同步
  • 自动驾驶电车难题的康德式道德决策
  • 黑马头条day6-kafka及异步通知文章上下架
  • Spring 全家桶使用教程 —— 后端开发从入门到精通
  • C#——switch案例讲解
  • 计算机毕业设计 校园失物招领网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 58 深层循环神经网络_by《李沐:动手学深度学习v2》pytorch版
  • 【论文写作】描述一个模型比另一个模型效果好时
  • sentinel原理源码分析系列(二)-动态规则和transport
  • 如何在openEuler上安装和配置openGauss数据库
  • linux编辑文件保存退出的实操讲解