Finally I managed to export my drawing in .png to Android. However, the Drawing folder is found in Document and not Pictures. Here is the code that needed to be modified to export to Android.
func request_storage_permissions():
if OS.get_name() == "Android":
# In Godot 4.3, permissions are handled differently
# You need to declare them in the export template or project settings
OS.request_permissions()
func export_drawing_to_png():
var tile_size = tilemap_layer.tile_set.tile_size
var tilemap_size = (max_bounds - min_bounds + Vector2i.ONE) * tile_size
# Create the viewport for capturing the TileMap rendering
var viewport := SubViewport.new()
viewport.size = tilemap_size
viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_ALWAYS
viewport.render_target_update_mode = SubViewport.UPDATE_ONCE
viewport.transparent_bg = false
viewport.disable_3d = true
# Duplicate the TileMap into a temporary container
var container := Node2D.new()
var tilemap_copy := tilemap_layer.duplicate()
tilemap_copy.position = -min_bounds * tile_size # Center at the origin
container.add_child(tilemap_copy)
viewport.add_child(container)
# Add the viewport to the scene so it gets rendered
add_child(viewport)
await RenderingServer.frame_post_draw # Wait for the rendering to complete
# Retrieve the image from the viewport texture
var image = viewport.get_texture().get_image()
# Clean up
remove_child(viewport)
viewport.queue_free()
# Build the save path - Use user:// for Android compatibility
var base_dir: String
if OS.get_name() == "Android":
# On Android, use the app's internal storage or documents directory
base_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS)
if base_dir.is_empty():
# Fallback to user:// directory (app's internal storage)
base_dir = OS.get_user_data_dir()
else:
# On other platforms, use the Pictures directory
base_dir = OS.get_system_dir(OS.SYSTEM_DIR_PICTURES)
var drawing_dir := base_dir.path_join("Drawing")
# Ensure the "Drawing" folder exists
if not DirAccess.dir_exists_absolute(drawing_dir):
var dir_result = DirAccess.make_dir_recursive_absolute(drawing_dir)
if dir_result != OK:
print("ā Failed to create directory:", drawing_dir, " Error:", dir_result)
return
# Create a filename with a valid timestamp
var timestamp := Time.get_datetime_string_from_system().replace(":", "-").replace(" ", "_")
var filename := "export_tilemap_" + timestamp + ".png"
var file_path := drawing_dir.path_join(filename)
print("š Attempting to save to:", file_path)
# Save the image as PNG
var err := image.save_png(file_path)
if err == OK:
print("ā
Export PNG succeeded:", file_path)
# On Android, also try to copy to a more accessible location
if OS.get_name() == "Android":
var accessible_path = "/storage/emulated/0/Pictures/Drawing/" + filename
var copy_err = image.save_png(accessible_path)
if copy_err == OK:
print("ā
Also saved to accessible location:", accessible_path)
else:
print("ā ļø Could not save to accessible location:", copy_err)
else:
print("ā Error during PNG export:", err)
print("š Tried path:", file_path)
On the other hand, I would like to put the Drawing folder in Picture and not Document.