# PNG file save (SOLVED)

**URL:** <https://forum.defold.com/t/png-file-save-solved/12005>\
**Category:** Questions\
**Created:** [September 29, 2017, 6:45am UTC](https://forum.defold.com/t/png-file-save-solved/12005 "2017-09-29T06:45:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HG17](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hg17/32/4757_2.png) [@HG17](https://forum.defold.com/u/HG17)\
**Post date:** [September 29, 2017, 6:45am UTC](https://forum.defold.com/t/png-file-save-solved/12005/1 "2017-09-29T06:45:26Z")

</div>

Hi guys~!

I was in the process of loading the resources used by gui from my private server using Live Update. There is too much atlas used by gui (about 1440x800 pixels with more than 100), causing the editor to stop.

So I decided to change the way. As soon as I run apk, I get the resources used by the gui from my private server. Below is a function that loads an image from the server.

```lua
function list_image_download(self, list_table)
	
	local image_table = {}
	
	for i = 1, #list_table do
		
		-- 0 is not exist, 1 is exist
		if list_table[i].exist == 0 then
			http.request(URL .. "images/" .. list_table[i].name, "GET", function(self, _, response) 
				
				if response.status >= 200 or response.status < 300 then
				
					local image = image.load(response.response)
					
					if not image then
						print(list_table[i].name .. " unable load")
						return
					end
					
					list_table[i].exist = 1
					
					-- i need a save process ..
				else
					print("IMAGE RESOURCE LOADING FAILED")
					return
				end
			
			end, headers)
		end	
	end
end

```

I succeeded in downloading the image, but could not proceed to the next. Can I save this png file to my phone? In addition, what happens to the saved files when I delete the application? Help me ~~ 😭

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 29, 2017, 9:32am UTC](https://forum.defold.com/t/png-file-save-solved/12005/2 "2017-09-29T09:32:40Z")

</div>

> [@HG17](#):
>
> Can I save this png file to my phone? In addition, what happens to the saved files when I delete the application?

Yes, if you use the Lua io.\* functions you can save and load files. Untested example:

```
local f = io.open("foo.png", "wb")
f:write(response.response)
f:flush() -- possibly also flush before closing
f:close()

local f = io.open("foo.png", "rb")
local s = f:read("*a")
f:close()
image.load(s)

```

---

<div class="post-metadata">

**Author:** ![HG17](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hg17/32/4757_2.png) [@HG17](https://forum.defold.com/u/HG17)\
**Post date:** [October 2, 2017, 7:35am UTC](https://forum.defold.com/t/png-file-save-solved/12005/3 "2017-10-02T07:35:11Z")

</div>

Sorry for the late reply. 😅

> [@Defold PNG - Inspect, load and save PNG files](https://forum.defold.com/t/defold-png-inspect-load-and-save-png-files/8832):
>
> Not really sure how useful this is but I thought I’d share it anyway. [Defold PNG](https://github.com/britzl/defold-png) is a Defold native extension that allows you to inspect, load and save PNG files. Usage: -- load my.png from disk local f = io.open("my.png", "rb") local bytes = f:read("\*a") -- read the PNG header local info = png.info(bytes) print(info.width) print(info.height) print(info.colortype) -- png.RGB, png.RGBA, png.GREY, png.GREY\_ALPHA or png.PALETTE print(info.bitdepth) -- decode the PNG to a Defold buffer in RGB(A) …

I have read and completely solved this. Thanks britzl!! 😁

---

<div class="post-metadata">

**Author:** ![HG17](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hg17/32/4757_2.png) [@HG17](https://forum.defold.com/u/HG17)\
**Post date:** [October 6, 2017, 8:30am UTC](https://forum.defold.com/t/png-file-save-solved/12005/4 "2017-10-06T08:30:43Z")

</div>

```lua
local f = io.open("foo.png", "wb")
f:write(response.response) -- error here

```

Io.open works on PC but does not work on Android.

![image](https://forum-defold.b-cdn.net/uploads/default/original/2X/3/357864d0c0efd2ead29888da517d46416b132fb9.png)

What permissions do I need on Android?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 6, 2017, 10:45am UTC](https://forum.defold.com/t/png-file-save-solved/12005/5 "2017-10-06T10:45:49Z")

</div>

You need to write to a folder where the user and the app has write permissions. Change to:

```
local path = sys.get_save_file("myapp", "foo.png")
local f = io.open(path, "wb")

```

Note that “myapp” should be replaced by a name unique to your game! Perhaps use the project title (and maybe get it as `sys.get_config("project.title")`

---

<div class="post-metadata">

**Author:** ![HG17](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hg17/32/4757_2.png) [@HG17](https://forum.defold.com/u/HG17)\
**Post date:** [October 11, 2017, 5:43am UTC](https://forum.defold.com/t/png-file-save-solved/12005/6 "2017-10-11T05:43:47Z")

</div>

Thank you for all your help. @britzl!! 😁  
I downloaded png, saved it, and loaded it. But I also have two questions…

- The first is the ‘WARNING: GUI: Failed to create dynamic gui texture (-6)’ issue in gui.new\_texture. I’m developing the action to bring the stored image when I touch it. The first touch will be gui.set\_texture, but it will not work after that.

> WARNING:GUI: Failed to create dynamic gui texture (-6)  
> DEBUG:SCRIPT: ICON NEW TEXTURE ERROR!

```lua
local path = sys.get_save_file("image_speaker", icon .. ".png")
	local f = io.open(path, "rb")
	local s = f:read("*a")
		
	f:close()
	local img = image.load(s)
	
	if gui.new_texture("icon", img.width, img.height, "rgba", img.buffer) then -- error here
		
		gui.set_texture(node, "icon")
		
	else
		print("ICON NEW TEXTURE ERROR!")
	end

```

- The second is about the image type. I downloaded a png file with an alpha value, but the type of the saved png file is rgb. Is there a problem saving the image?

 ![1005601](https://forum-defold.b-cdn.net/uploads/default/original/2X/e/eff933feda40885a2d9b583f1d7264cadd3b9853.jpg)  
(my image)  
 ![image](https://forum-defold.b-cdn.net/uploads/default/original/2X/d/d78177fff10a48d703d9eea09bbaa649e4c9a5e4.jpg)  
(in game image)

This is a image save code.

```lua
http.request(URL .. "images/speaker/" .. speaker_data[i].name .. ".png", "GET", function(self, _, response) 
				
		if response.status >= 200 or response.status < 300 then
					
			local path = sys.get_save_file("image_speaker", speaker_data[i].name .. ".png")
			local f = assert(io.open(path, "wb"))
					
			f:write(response.response)
			f:flush()
			f:close()
					
			self.currentdownload = self.currentdownload + 1
			print(self.currentdownload)
					
		end
	end, headers)

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 11, 2017, 6:43am UTC](https://forum.defold.com/t/png-file-save-solved/12005/7 "2017-10-11T06:43:37Z")

</div>

> [@HG17](#):
>
> The first is the ‘WARNING: GUI: Failed to create dynamic gui texture (-6)’ issue in gui.new\_texture.

> [@HG17](#):
>
> gui.new\_texture(“icon”, img.width, img.height, “rgba”, img.buffer)

“icon” used in gui.new\_texture() represents the texture id. This must be unique and -6 indicates that there already is a texture with that id. You have a couple of options:

1. Call gui.delete\_texture()
2. Reuse the same texture and only call gui.set\_texture\_data()
3. Select another texture id

> [@HG17](#):
>
> I downloaded a png file with an alpha value, but the type of the saved png file is rgb.

Hmm, this sounds strange. If you are downloading using http.request and saving using io.write then there is no way that the transparency could get lost.

---

<div class="post-metadata">

**Author:** ![HG17](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hg17/32/4757_2.png) [@HG17](https://forum.defold.com/u/HG17)\
**Post date:** [October 11, 2017, 9:57am UTC](https://forum.defold.com/t/png-file-save-solved/12005/8 "2017-10-11T09:57:53Z")

</div>

Perfect!🤣🤣 The first issue was resolved by reusing the texture, and the second issue was the problem with the original image file. You are my hero. Thanks @britzl!
