首页 > 解决方案 > 将位图保存到 PNG 文件随机变黑

问题描述

我正在从相机预览中处理大约 4 张图像/秒,将其保存为 png 文件并将其发送到服务器。

我还无法确定原因,但当前会话中有 1 次会将 .png 存储为全黑图片。文件大小也非常小(3kb 与通常的 700kb)。

这是我将位图存储为 PNG 文件的方法:

public static File SaveImagePNG(Bitmap subimg, String path, String filename) {
        FileOutputStream out = null;

    File sd = new File(path);
    boolean success = true;
    if (!sd.exists()) {
        success = sd.mkdir();
    }
    if (success) {
        File dest = new File(sd, filename);

        try {
            out = new FileOutputStream(dest);
            if (subimg != null) {
                // PNG is a lossless format, the compression factor (100) is ignored
                subimg.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, e.getMessage());
        } finally {
            try {
                if (out != null) {
                    out.close();
                    Log.d(TAG, "OK!!");
                }
            } catch (IOException e) {
                Log.d(TAG, e.getMessage() + "Error");
                e.printStackTrace();
            }
        }
        return dest;
    }
    return null;
}

这就是我所说的:

        val pathFilePNG = Environment.getExternalStorageDirectory().toString() + "/images"

        requestParams[0]?.bitmapRGB?.setHasAlpha(false)
        val imageFaceRGBFile = BitmapUtils.SaveImagePNG(requestParams[0]?.bitmapRGB, pathFilePNG, "faceRGB.png")

标签: androidandroid-bitmap

解决方案


推荐阅读