- Home /
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?
[edit] 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){
//?
}
Tthere is still no solution to the above problem? I have the same problem...
I've solved it using this cheap asset: https://www.assetstore.unity3d.com/en/#!/content/38872
Answer by tarasfromlviv · Jan 07, 2017 at 12:45 PM
Hello guys, to get onActivityResult
result callback you will have to write a plugin - there are two ways of achieving this:
Overriding`UnityPlayerActivity` and overriding
onActivityResult
there - I don't recommend it as it most of the times complicates the build process immenselyWriting you own activity which handles the
onActivityResult
callback - launching it, doing the job inonCreate
and killing it when the job is done (image passed to the C# code)
I also wrote an article how to do that: https://medium.com/@tarasleskiv/unity-android-plugins-and-onactivityresult-callback-abef4b6bbc87#.53gj04ean
I am using this approach in my plugin exactly for this purpose.
Your answer
![](https://koobas.hobune.stream/wayback/20220611232212im_/https://answers.unity.com/themes/thub/images/avi.jpg)