Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image.save_png("user://image_name.png") does not save image in reachable location in Android export #45467

Closed
ghost opened this issue Jan 26, 2021 · 7 comments · Fixed by #50360

Comments

@ghost
Copy link

ghost commented Jan 26, 2021

Godot version:

3.2.3

OS/device including version:

Pixel 2, Android 9

Issue description:

Tried saving an image in the user directory with Image.save_png(). It didn't work. I enabled read/write to external storage perms and asked the user to grant them (the popup appeared). I printed the error code and it was 0. I went into my files but I don't see anything that looks like the image I saved.

Edit: Found the file with adb, but I'm not sure how to get to it by using my phone. I want the images to be saved in a place that the user can easily access them.

Steps to reproduce:
Get the data of the texture of the default icon and save it with Image.save_png().

Minimal reproduction project:

Test.zip

@ghost ghost changed the title Image.save_png() does not save image in Android export Image.save_png("user://image_name.png") does not save image in reachable location in Android export Jan 26, 2021
@dalexeev
Copy link
Member

Use the OS.get_system_dir() method (docs). user:// paths don't fit here. On Android, the root user-accessible directory is usually /storage/emulated/0.

@Calinou
Copy link
Member

Calinou commented Jan 26, 2021

See also #39414.

@ghost
Copy link
Author

ghost commented Jan 26, 2021

Okay, so here's how I'm trying to save the image:

1 - Save to "user://".

Result:
Image is saved to "/data/data/org.godotengine.nameofproject/files/image.png".

How to access from phone:
No clue.

2 - Save to OS.get_system_dir(OS.SYSTEM_DIR_PICTURES).

Result:

E 0:00:31.060   save_image: Can't saveNG at path: '/storage/emulated/0/Pictures'.
  <C++ Error>   Condition "err" is true. Returned: err
  <C++ Source>  drivers/png/resource_saver_png.cpp:58 @ save_image()
  <Stack Trace> Global.gd:181 @ save_file()

How to access from phone:
Pretty sure this isn't an actual path but a representation, it says "emulated" in the path. I printed OS.get_granted_permissions() and yes I have read/write external storage perms.
I printed out the error code and it was 12 --- File: Can't open error.

@ghost
Copy link
Author

ghost commented Jan 27, 2021

I have managed to save the image in a reachable location, finally. I eventually ended up creating a new directory recursively at "/sdcard/Android/data/org.godotengine.nameofproject/files/" and saved the image there. It seems to also be deleted automatically when I uninstall the app as well.

@Calinou
Copy link
Member

Calinou commented Jan 27, 2021

To solve this, should we make Image.save_png() and Image.save_exr() create directories recursively if the target directory doesn't exist?

@volzhs
Copy link
Contributor

volzhs commented Jan 27, 2021

we have already Directory.make_dir_recursive(path).
I think this issue is about documentation for "How to save a visible custom file to other apps on Android?"

@ghost
Copy link
Author

ghost commented Jan 27, 2021

Yeah, that was my issue. I couldn't find an answer to that in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants