Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Android onActivityResult Callback for Unity

Discussion in 'Android' started by jbvobling, Jan 4, 2019.

  1. jbvobling

    jbvobling

    Joined:
    Sep 26, 2017
    Posts:
    27
    This is a sample code of sharing an image file and open the sharing UI.
    =============================================================================
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    intentObject.Call<AndroidJavaObject>("setType", GetSetType(setType) );

    AndroidJavaClass fileProviderClass = new AndroidJavaClass("android.support.v4.content.FileProvider");
    AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");

    string packageName = unityContext.Call<string>("getPackageName");
    string authority = packageName + ".provider";

    string[] paths = new string[] { Application.persistentDataPath + "/CustomFiles/" + fileName };
    Debug.Log(paths[0]);

    AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", paths[0]);
    AndroidJavaObject uriObj = fileProviderClass.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);
    int FLAG_GRANT_READ_URI_PERMISSION = intentObject.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");
    intentObject.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObj);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObj);
    AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "");
    currentActivity.Call("startActivity", jChooser);
    =============================================================================
    This code works but my problem is that, I don't know how to call the onActivityResult callback. Anyone know what line of code must be used to make it work?
     
    asinghmodesttree likes this.
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,158
    asinghmodesttree likes this.
  3. jbvobling

    jbvobling

    Joined:
    Sep 26, 2017
    Posts:
    27
    I tried this but nothing happens

    I changed currentActivity.Call("startActivity", jChooser);
    to
    currentActivity.Call("startActivityForResult", jChooser, 1);

    Then, I create a subclass:

    class UnityPlayerActivity : AndroidJavaProxy
    {
    public UnityPlayerActivity() : base("com.unity3d.player.UnityPlayer") { }

    public void onActivityResult(int requestCode, int resultCode, AndroidJavaClass resultIntent)
    {
    Debug.Log("test");
    }
    }

    it's not working. Any ideas?
     
    asinghmodesttree likes this.
  4. NinjaCat

    NinjaCat

    R&D Minion Unity Technologies

    Joined:
    Aug 22, 2013
    Posts:
    69
    com.unity3d.player.UnityPlayer is not an activity. You might want to take a look into the source of UnityPlayerActivity.java (it can be found in Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player of your unity installation).

    What you probably want is to extend the UnityPlayerActivity and not the UnityPlayer itself. You should also make sure that the extended activity is used instead of the default one.
     
    asinghmodesttree likes this.
  5. rahulrathaurkz

    rahulrathaurkz

    Joined:
    Dec 11, 2020
    Posts:
    1
    Can You Please Give Me A Example
     
unityunity