短视频app开发,短视频动态功能上传图片时,规定图片压缩的大小

短视频app开发,短视频动态功能上传图片时,规定图片压缩的大小

 

/**
     * 保存图片到指定路径
     * Save image with specified size
     *
     * @param filePath the image file save path 储存路径
     * @param bitmap   the image what be save   目标图片
     * @param size     the file size of image   期望大小
     */
    private static File saveImage(String filePath, Bitmap bitmap, long size) {
        File result = new File(filePath.substring(0, filePath.lastIndexOf("/")));
        if (!result.exists() && !result.mkdirs()) return null;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        int options = 100;
        bitmap.compress(CompressFormat.JPEG, options, stream);
        while (stream.toByteArray().length / 1024 > size && options > 6) {
            stream.reset();
            options -= 6;
            bitmap.compress(CompressFormat.JPEG, options, stream);
        }
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(stream.toByteArray());
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new File(filePath);
    }

以上就是短视频app开发,短视频动态功能上传图片时,规定图片压缩的大小, 更多内容欢迎关注之后的文章

 

posted @ 2021-11-08 14:19  云豹科技-苏凌霄  阅读(67)  评论(0编辑  收藏  举报