Android drawBitmap()和/或compress()只写入图像的一部分(Android drawBitmap() and/or compress() only writes part o

编程入门 行业动态 更新时间:2024-10-27 11:22:17
Android drawBitmap()和/或compress()只写入图像的一部分(Android drawBitmap() and/or compress() only writes part of image)

我正在尝试将位图导出到PNG文件,但每当我打开PNG文件时,它只会渲染前几行。 我简化了我的代码,只是将青色的颜色绘制到位图,但是,在导出时,它只绘制了图像的前1/8或1/4。

也就是说,我的canvasWidth和canvasHeight是2560x1600,Galaxy Note 10.1 2014 Ed的大小,只有顶部349左右的像素线渲染青色,而其余的是空白。

Bitmap bmp = Bitmap.createBitmap(canvasWidth, canvasHeight, Config.ARGB_8888); Canvas canvas = new Canvas(bmp); canvas.drawColor(Color.CYAN); FileOutputStream fs = null; try { fs = new FileOutputStream(f); if (!bmp.compress(CompressFormat.PNG, 100, fs)) { Toast.makeText(context, "Compression failure", Toast.LENGTH_LONG).show(); errorCode = ERR_STREAM_CORRUPTED; } } catch (FileNotFoundException e) { errorCode = ERR_FILE_NOT_FOUND; } catch (IOException e) { errorCode = ERR_IO_EXCEPTION; } finally { try { if (fs != null) { fs.flush(); fs.close(); } } catch (IOException e) { errorCode = ERR_IO_EXCEPTION; } }

在压缩和写入文件流时,我是否有完全写入内存的位图问题? 我似乎无法弄清楚为什么这不起作用。

I'm trying to export a bitmap to a PNG file, but whenever I open the PNG file, it only has the first few lines rendered. I've simplified my code to simply draw the color cyan to the bitmap, yet, when exporting, it only draws about the top 1/8th or 1/4th of the image.

i.e., My canvasWidth and canvasHeight are 2560x1600, the size of the Galaxy Note 10.1 2014 Ed, and only the top 349 or so lines of pixels render the cyan, while the rest is blank.

Bitmap bmp = Bitmap.createBitmap(canvasWidth, canvasHeight, Config.ARGB_8888); Canvas canvas = new Canvas(bmp); canvas.drawColor(Color.CYAN); FileOutputStream fs = null; try { fs = new FileOutputStream(f); if (!bmp.compress(CompressFormat.PNG, 100, fs)) { Toast.makeText(context, "Compression failure", Toast.LENGTH_LONG).show(); errorCode = ERR_STREAM_CORRUPTED; } } catch (FileNotFoundException e) { errorCode = ERR_FILE_NOT_FOUND; } catch (IOException e) { errorCode = ERR_IO_EXCEPTION; } finally { try { if (fs != null) { fs.flush(); fs.close(); } } catch (IOException e) { errorCode = ERR_IO_EXCEPTION; } }

Do I have a problem with the bitmap not being fully written to memory by the time I'm compressing and writing to the file stream? I can't seem to figure out why this isn't working.

最满意答案

事实证明Bitmap.compress()正如预期的那样工作。 我的问题是我依靠三星的文件浏览桌面实用程序在文件写出时进行更新。 这是禁忌。 桌面浏览工具在这种情况下是非常错误的,并且使用设备上的图像查看应用程序检查PNG文件(我使用QuickPic并且现在完全支持它)显示了真实的文件输出。

Turns out that Bitmap.compress() was working as expected. My problem was that I was relying on Samsung's file browsing desktop utility to be updated as the files were written out. This is a no-no. The desktop browsing util was extremely erroneous in this case, and checking the PNG file out with an image viewing app on the device (I used QuickPic and fully endorse it now) showed the real file output.

更多推荐

本文发布于:2023-07-28 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   compress   drawBitmap   Android   part

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!