Search Unity

Android Plugin for Chooser not working

Discussion in 'Android' started by gfxguru, Nov 7, 2017.

  1. gfxguru

    gfxguru

    Joined:
    Jun 5, 2010
    Posts:
    107
    I was able to write some code based on the java codes for showing a file open chooser. I need this to give the user to choose application of choice for opening a pdf file. The code works but the problem is the chooser is never shown, but the pdf is directly opened in the default pdf application on my device, that is unfortunately Google Drive app and not the pdf. I have less experience in android plugins, so can somebody look at this code and see what is wrong?

    Why is it not opening the chooser dialog and going directly to opening the file?
    Thanks in advance

    Code (CSharp):
    1. public void OpenFile(){
    2.        
    3.         OpenInExternalApp (Application.persistentDataPath + "/test.pdf", "application/pdf");
    4.     }
    5.  
    6.     public static void OpenInExternalApp(string filePath,string fileType){
    7.  
    8.         using (AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent"))
    9.         using (AndroidJavaObject intentObject = new AndroidJavaObject ("android.content.Intent")) {
    10.  
    11.             string finalPath ="file://"+ filePath;
    12.             Debug.Log (finalPath);
    13.  
    14.             using (intentObject.Call<AndroidJavaObject> ("setAction", intentClass.GetStatic<string> ("ACTION_VIEW")))
    15.             using (intentObject.Call<AndroidJavaObject> ("setType", fileType))
    16.             using (AndroidJavaClass uriClass = new AndroidJavaClass ("android.net.Uri"))
    17.                
    18.             using (AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject> ("parse", finalPath))
    19.             using (intentObject.Call<AndroidJavaObject> ("setData", uriObject))
    20.                 Debug.Log ("Created java objects");
    21.             // finally start application
    22.             using (AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"))
    23.             using (AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity")) {
    24.                 Debug.Log ("launching chooser");
    25.  
    26.                 AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject> ("createChooser", intentObject, "Open In");
    27.  
    28.                 currentActivity.Call("startActivity", jChooser);
    29.             }
    30.  
    31.  
    32.         }
     
    dan_ginovker likes this.
  2. yusufix1

    yusufix1

    Joined:
    Nov 1, 2023
    Posts:
    1
    AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaClass Uri = new AndroidJavaClass("android.net.Uri");
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");

    AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
    AndroidJavaObject uri = Uri.CallStatic<AndroidJavaObject>("parse", "LINK URL");

    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_VIEW"));
    intentObject.Call<AndroidJavaObject>("setPackage", "APPNAME com.xxx.xxxx");
    intentObject.Call<AndroidJavaObject>("setType", "application/x-mpegURL");
    intentObject.Call<AndroidJavaObject>("setData", uri);


    ca.Call("startActivity", intentObject);