Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

about onActivityResult

Discussion in 'Android' started by wwapw29, Mar 30, 2020.

  1. wwapw29

    wwapw29

    Joined:
    Jul 20, 2018
    Posts:
    1
    Hello everyone, I want to open Android album and upload photos in my game for some reasons.

    And for some reason, I can't use eclipse's jar package or Android studio's arr package.
    So I wrote the following code in the C# script:
    Code (CSharp):
    1. public class AndroidCtl : MonoBehaviour
    2. {
    3.     AndroidJavaClass Target;
    4.     AndroidJavaObject jo;
    5.     AndroidJavaClass uri;
    6.    
    7.     private void Start()
    8.     {
    9.         DontDestroyOnLoad(gameObject);
    10.         Target = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    11.         jo = Target.GetStatic<AndroidJavaObject>("currentActivity");
    12.         uri = new AndroidJavaClass("android.net.Uri");
    13.  
    14.        
    15.         OpenGallery();
    16.     }
    17.  
    18.     public void ThridLog(string message)
    19.     {
    20.         Debug.Log(message);
    21.     }
    22.     public void OpenGallery()//
    23.     {
    24.         // Target.Call("OpenGallery");
    25.         AndroidJavaObject uriObject = uri.CallStatic<AndroidJavaObject>("parse", "content://media/external/images/media");
    26.         AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", "android.intent.action.PICK", null);
    27.         intent.Call<AndroidJavaObject>("setDataAndType", uriObject, "image/*");
    28.         jo.Call("startActivityForResult", intent, 2);
    29.     }
    30.  
    31. }

    The code is running fine
    But I need to get the photo address of my choice after the album activity is closed.

    So i wrote this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AndroidPox : AndroidJavaProxy
    6. {
    7.  
    8.     public AndroidPox() : base("com.unity3d.player.UnityPlayerActivity") { }//
    9.                                                                                            
    10.  
    11.     public void onActivityResult(int requestCode, int resultCode, AndroidJavaClass resultIntent)
    12.     {
    13.         Debug.Log("test");
    14.     }
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.     }
    27. }
    Now i konw they won't working because the onActivityResult is NOT in a interface and my AndroidPox's base must be a adress of interface.

    Does anyone know how I can implement my function? Or can I only make jar package or arr package?
     
  2. unity_F62923F8454F0E208D27

    unity_F62923F8454F0E208D27

    Joined:
    Sep 20, 2022
    Posts:
    2
    Sorry to reopen and old thread, but there was no resolution. Did you eventually get this to work this way?