How to import a .png using intents on Android?

For my game I want to import and export levels the player has made in the editor.
I’ve figured out how to do the export from Application.persistentDataPath to Dropbox/gallery using Intents, but cannot seem to find how to receive an image using Intents.

As far as I can tell so far, the basics is the same, but instead of ACTION_SEND I use ACTION_GET_CONTENT, but I have no idea where the image is then sent. All examples I find are for native Android apps and those seem to call a function when an image is selected, but I’m unsure how to deal with this on Android.

Could someone provide some insight?

So far I seem to need to find out how to call startActivityForResult instead of startActivity, and where/how to override onActivityResult. Everything I find is people writing plugins for Unity, but I’d like to keep it to C# scripts. If I can use intents, then this must be possible too right?

What I have so far:

	void OnMouseDown(){
		AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent");
		AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent");
		intentObject.Call<AndroidJavaObject> ("setType", "image/png");
		intentObject.Call<AndroidJavaObject> ("setAction", intentClass.GetStatic<string> ("ACTION_GET_CONTENT"));
		AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
		AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
		currentActivity.Call ("startActivityForResult", 
		                      intentObject.Call<AndroidJavaObject>(intentClass.CallStatic(	"createChooser", intentObject, "Select picture")), 
		                      intentClass.GetStatic<string> ("SELECT_IMAGE"));

	void onActivityResult(string resultData){

Hello guys, to get onActivityResult result callback you will have to write a plugin - there are two ways of achieving this:

  • OverridingUnityPlayerActivity and overriding onActivityResult there - I don’t recommend it as it most of the times complicates the build process immensely
  • Writing you own activity which handles the onActivityResult callback - launching it, doing the job in onCreate and killing it when the job is done (image passed to the C# code)

I also wrote an article how to do that: Unity Android Plugins and `onActivityResult` callback | by Taras Leskiv | nineva | Medium

I am using this approach in my plugin exactly for this purpose.