安卓保存图片到本地


 val imgBitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.size)

saveBitmapToSDCard(imgBitmap ,"sd path")


    /**
     * 转码处理
     * @param data
     * @param width 图片宽度
     * @param height 图片高度
     * @return
     */
    private fun transcodeProcess(data: ByteArray, width: Int, height: Int): ByteArray {
        val yuvImage = YuvImage(data, ImageFormat.NV21, width, height, null)
        val baos = ByteArrayOutputStream()
        yuvImage.compressToJpeg(Rect(0, 0, width, height), 80, baos)
        return baos.toByteArray()
    }
    /**
     * 将bitmap对象保存成图片到sd卡中
     */
    public static void saveBitmapToSDCard(Bitmap bitmap, String path) {

        try {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            } else {
                new File(path.substring(0, path.lastIndexOf("/") + 1)).mkdirs();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.close();
            MLog.d("save success  " + path);
        } catch (Exception v0) {
            v0.printStackTrace();
        }

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容