PNG file save (SOLVED)

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.

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 ~~ :sob:

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)
2 Likes

Sorry for the late reply. :sweat_smile:

I have read and completely solved this. Thanks britzl!! :grin:

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

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

What permissions do I need on Android?

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")

Thank you for all your help. @britzl!! :grin:
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!

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
(my image)
image
(in game image)

This is a image save code.

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)
1 Like

“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

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.

3 Likes

Perfect!:rofl::rofl: 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!

3 Likes