Import image from Android in c#

I’ve been looking around to find a way to import images from the Android gallery into Godot, but the only thing I found was this plugin that works in GdScript.

Is there also a C# version of this?

2 Likes

Godot Android plugins can be used with both gdscript and c#, yes the demo is written in gdscript but you can convert it to c# either by using a tool or converting it manually.
The godot docs have an incomplete list of API differences that can help you convert the project.

Below is an example of how to get the plugin singleton:

	private object plugin;
	private string pluginName = "GodotGetImage";
	public override void _Ready()
	{
		if (Engine.HasSingleton(pluginName))
		{
			plugin = Engine.GetSingleton(pluginName);
		}
		else
		{
			GD.Print("Could not load plugin: ", pluginName);
		}
	}

Am I only supposed to convert the export_plugin.gd file, or do I need to change a few things within the java code of the plugin as well?

image

Also, do I need to change anything about the plugin.cfg file aside from changing

script=“export_plugin.gd”

to

script=“ExportPlugin.cs”

after I finish converting?

No, you don’t need to change anything with the plugin itself. What you need is a script that can handle most of the plugin, I’ve created a small one for you to build from.

// androidCamera.cs
using Godot;
using Godot.Collections;
namespace androidCamera; // or your project namespace

public partial class androidCameraHandler : Node
{

	private GodotObject plugin;
	private string pluginName = "GodotGetImage";
	public override void _Ready()
	{
		if (Engine.HasSingleton(pluginName))
		{
			plugin = Engine.GetSingleton(pluginName);
		}
		else
		{
			GD.Print("Could not load plugin: ", pluginName);
		}
		if (plugin != null)
		{
			plugin.Connect("image_request_completed", new Callable(this,"onImageRequestCompleted"));
			plugin.Connect("error",new Callable(this,"onImageError"));
			plugin.Connect("permission_not_granted_by_user", new Callable(this,"onPermissionNotGrantedByUser"));
		}

	}
	public void onImageRequestCompleted(Dictionary dict)
	{
		if (plugin != null)
		{
			// handle image(s)
			GD.Print(dict);
		}
	}
	public void onImageError(string error)
	{
		if (plugin != null)
		{
			//handle error
			GD.Print(error);
		}
	}
	public void onPermissionNotGrantedByUser()
	{
		if (plugin != null)
		{
			plugin.Call("resendPermission");
		}
	}
	public void requestImage()
	{
		if (plugin != null)
		{
			plugin.Call("getGalleryImage");
		}
	}
	public void requestImages()
	{
		if (plugin != null)
		{
			plugin.Call("getGalleryImages");
		}
	}
	public void requestImageFromCamera()
	{
		if (plugin != null)
		{
			plugin.Call("getCameraImage");
		}
	}
}

I am getting the message “Could not load plugin: GodotGetImage” Even though I have it enabled.

image
main node


result


my plugins

Do I need to change export_plugin.gd to a c# file to fix this?
image

Are you running your app/game on a PC or Android phone? If you’re running it on a PC you will get that error as the plugin only runs on Android. You need to run it on an Android phone or Android emulator.

It took forever to get the right java version for the export ( JDK 17 for anyone else wondering ), but after installing the apk on my phone it worked.
I was asked for the permissions, and the return value for installation was positive.
Thanks.

1 Like