Search Unity

Application.OpenURL on Unity 2017.2 + Android 7 not working

Discussion in 'Android' started by martejpad, Oct 28, 2017.

  1. martejpad

    martejpad

    Joined:
    May 24, 2015
    Posts:
    23
    Hello everyone,

    I've been using Application.OpenURL() successfully so far using Unity 5.5.0 + Android API 23 (Marshmallow) to open a PDF that's stored in the application's persistentDataPath. This setup worked for devices with both Marshmallow and Nougat installed. However, after updating Unity to version 2017.2 and updating Android's SDK to API 25 this has stopped working on devices with Nougat. It seems like the instruction is ignored altogether, as no errors appear on the log. It still works normally with devices using Marshmallow.

    I'd be extremely grateful if someone could shed some light to this problem, as I really have tried everything. Perhaps there's a security setting/update that's messing this up?

    Many thanks for your help,
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    If it works fine in earlier Unity versions, but stops working in 2017.2, then you most likely found a regression.

    In this case, I recommend to submit a bug-report following the advice in this document.

    Using the bug-reporter seems to be an important step, because it makes sure the report is in Unity Technologies bug-tracking pipeline and has to be processed at some point. Using the forum is often used to add to a little more attention to a bug-report, but does not replace submitting the bug-report.

    It's from advantage to attach a project to the bug-report that UT can use to reproduce the issue and test their fix against. The easier an issue can be reproduced by QA, the more likely it is to get forwarded to a developer, who might or might not work on a bug-fix for that at some point.

    After you submitted the bug-report, you receive a confirmation email with a Case number. UT often asks us to post the Case number in the forum thread, which allows them to find that bug-report if they look at your post.

    Following these steps will increase the chance that UT is looking at your issue tremendously.
     
  3. martejpad

    martejpad

    Joined:
    May 24, 2015
    Posts:
    23
    Thank you very much for the advice, @Peter77. I will submit a bug report and hopefully they will have a look. While I was searching for answers I found this article regarding an important security update on Nougat that could be related to the problem:

    https://inthecheesefactory.com/blog...o-file-with-fileprovider-on-android-nougat/en

    Passing file:// URIs outside the package domain may leave the receiver with an unaccessible path. Therefore, attempts to pass a file:// URI trigger a FileUriExposedException. The recommended way to share the content of a private file is using the FileProvider.
    Perhaps this affects the way Application.OpenURL should be implemented for Android.

    Thanks again for your help
     
    Peter77 likes this.
  4. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    I had the same problem.

    You have to set your build settings for you app to a targetSdkVersion of 23 or lower to make it work ! Atfer it's a whole new system and OpenURL() doesn't work the same and it's not the only behviour that is affected.
     
    TobiWoVR likes this.
  5. Majingari

    Majingari

    Joined:
    Sep 8, 2015
    Posts:
    5
    Hi @martejpad , have you solve your problem? i had the same problem, and i cannot downgrade the SDK version because i'm using InApp Purchase Android Native. I'm stuck T.T,

    if you found the workaround, would be good to share here :)
     
  6. martejpad

    martejpad

    Joined:
    May 24, 2015
    Posts:
    23
    Hi @Majingari! I'm afraid I didn't find a solution even after a lot of searching... Downgrading the SDK version wasn't an option for me either. If you find a solution please share it here! Best of luck :D
     
  7. Rothanan

    Rothanan

    Joined:
    Oct 29, 2015
    Posts:
    1
    Any news on this? I just stumbled across the same problem...
     
  8. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
  9. LabInno

    LabInno

    Joined:
    May 19, 2016
    Posts:
    6
    I just get the same issue and no solution found..
     
    FiveFingers likes this.
  10. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    not even with Unity 2017.3 or .4 ?
     
  11. RobAtApex

    RobAtApex

    Joined:
    Jun 19, 2017
    Posts:
    21
    Bump, this was hours of time lost for me, as I'm sure it was for others, trying to track down the problem. Almost no posts about it either for some reason.

    I'm on 2017.1 right now. I can set targetSdkVersion to 23 safely I think, so that fixes it for me, for now. But has this been fixed in 2018.1 or 2018.2?

    Also, if I put "file://" before the path for OpenURL(), it complains that my JPG file is not a valid PDF! Why would "file://" presume the file was a PDF? Works if I remove the "file://".
     
  12. nicolasCxxii

    nicolasCxxii

    Joined:
    Dec 7, 2016
    Posts:
    1
    b4cksp4ce likes this.
  13. b4cksp4ce

    b4cksp4ce

    Joined:
    Apr 13, 2011
    Posts:
    114
    Still the problem in Unity 2018.2.2f1 (64-bit) !

    It works using the API 23, but the android store does not accept app compiled under 26 or lower..
     
  14. RobAtApex

    RobAtApex

    Joined:
    Jun 19, 2017
    Posts:
    21
    I just reported a bug for it. Not sure if anyone else already had. Might have more impact if others also reported the bug from their perspective?

    It's a show-stopper bug now, since very soon Google won't accept apps targeting below 26.
     
  15. benVisiativ

    benVisiativ

    Joined:
    Mar 8, 2018
    Posts:
    5
    Same problem here... No fix founded ...
     
  16. Evaldas_Unity

    Evaldas_Unity

    Unity Technologies

    Joined:
    Jun 23, 2016
    Posts:
    86
  17. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Well seems like Application.OpenURL() is used for things it was not initially designed for, like opening local files. Google has improved security in latest Android releases so that is not possible anymore. That's not a regression in Unity behavior.

    We'll look into what can be done to the issue.
     
  18. benVisiativ

    benVisiativ

    Joined:
    Mar 8, 2018
    Posts:
    5
    For those who are intested in a fix, I found this plugin to share image :
    https://github.com/yasirkula/UnityNativeShare
    I use the jar in the project which extend the FileProvider method to open a file.
    Follow the tutorial to install it then you can remove all but the jar and the manifest.

    My method to openfile (image there) :

    Code (CSharp):
    1.     private static AndroidJavaObject clazz = new AndroidJavaClass ("com.youractivity.yourname");
    2.     private static AndroidJavaObject activity = clazz.GetStatic<AndroidJavaObject> ("currentActivity");
    3.      
    4.     private static void OpenFileAndroid (string url)
    5.     {
    6.         using (var intent = new AndroidJavaObject("android.content.Intent"))
    7.         {
    8.             //permission to read URI
    9.             intent.Call<AndroidJavaObject>("addFlags", intent.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"));
    10.             intent.Call<AndroidJavaObject>("setAction", intent.GetStatic<string>("ACTION_VIEW"));
    11.  
    12.             //Get API Android Version
    13.             var versionClazz = new AndroidJavaClass("android.os.Build$VERSION");
    14.             var apiLevel = versionClazz.GetStatic<int>("SDK_INT");
    15.  
    16.             AndroidJavaObject uri;
    17.             if (24 <= apiLevel)
    18.             {
    19.                 //New version, need a fileprovider
    20.                 var context = activity.Call<AndroidJavaObject>("getApplicationContext");
    21.                 var fileProvider = new AndroidJavaClass("com.yasirkula.unity.UnitySSContentProvider");
    22.                 var file = new AndroidJavaObject("java.io.File", url);
    23.                 uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", context, YOURAUTHORITIESNAME, file);
    24.             }
    25.             else
    26.             {
    27.                 //Old version using uriClass
    28.                 var uriClazz = new AndroidJavaClass("android.net.Uri");
    29.                 var file = new AndroidJavaObject("java.io.File", url);
    30.                 uri = uriClazz.CallStatic<AndroidJavaObject>("fromFile", file);
    31.             }
    32.          
    33.             intent.Call<AndroidJavaObject>("setType", "image/*");
    34.             //set uri
    35.             intent.Call<AndroidJavaObject>("setData", uri);
    36.  
    37.             //start activity
    38.             activity.Call("startActivity", intent);
    39.         }
    40.     }
    I hope it helps
     
    Last edited: Oct 29, 2018
    ZeFirestarter and quyrean like this.
  19. konsnos

    konsnos

    Joined:
    Feb 13, 2012
    Posts:
    121
    hey people. I'm doing the same thing and opening a url from a file I just created. thing is this html file imports javascript, css and images that I also export, but I don't see them working in the html. Do I need to include them in the code above somehow?
     
  20. vijayxd

    vijayxd

    Joined:
    Aug 7, 2017
    Posts:
    10

    THanks for your code but im unable to use its not working...
    ("com.youractivity.yourname"); YOURAUTHORITIESNAME,

    can you describe these values how to change
     
  21. konsnos

    konsnos

    Joined:
    Feb 13, 2012
    Posts:
    121
    Here is the documentation of the getUriForFile. You can find an example for the FileProvider from this StackOverflow answer.
     
    msf-eng92 likes this.
  22. vijayxd

    vijayxd

    Joined:
    Aug 7, 2017
    Posts:
    10
    Thanks for your immediate response Konsnos.
    is this issue with URI address?? all i need is just to open local a file(image) externally. if i get uri address Application.url() will work?
     
  23. konsnos

    konsnos

    Joined:
    Feb 13, 2012
    Posts:
    121
    I linked this issue for its example of what you should write to the authorities parameter. No, this issue is not with URI address to open an image but an apk, so you should change values to reflect what you want to do.
     
  24. vijayxd

    vijayxd

    Joined:
    Aug 7, 2017
    Posts:
    10
    Yah I got it Thanks Konsnos
     
  25. vijayxd

    vijayxd

    Joined:
    Aug 7, 2017
    Posts:
    10
    tried all methods

    getting error
    fileuriexposedexception first method
    classnotfoundexception for second
    javalangnullpointerexpection
     
    Last edited: Dec 18, 2018
  26. xysquid

    xysquid

    Joined:
    Jun 15, 2017
    Posts:
    7
    I think most in this thread are talking about local files, but this might help someone with a similar problem who finds this page:

    On Android you need to use http:// in the URL

    Application.OpenURL("http://www.whatever.com");
     
    MostafaADev and VRAI_Chris like this.
  27. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    I would even say https://, since non-encrypted URLs are not directly supported by Android 9 anymore.
     
    kaisersam likes this.
  28. quyrean

    quyrean

    Joined:
    Nov 29, 2016
    Posts:
    16
    This worked great! Thanks. I was able to use the posted code with a few tweaks.

    1. I had to move the static variable into the function as I got a null exception when compiling them outside the function.
    2. replace "YOURAUTHORITIESNAME" with the authority I added to the andriod manafest xml
    3. replace "com.youractivity.yourname" with " com.unity3d.player.UnityPlayer"

    Here is the function beginning, the rest is the same except for replacing "YOURAUTHORITIESNAME"

    Code (CSharp):
    1.     ///create provider and junk for andriod
    2.     private static void OpenBrowserAndroid (string url)
    3.     {
    4.         using (var intent = new AndroidJavaObject("android.content.Intent"))
    5.         {
    6.             AndroidJavaObject clazz = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
    7.             AndroidJavaObject activity = clazz.GetStatic<AndroidJavaObject> ("currentActivity");
    8.  
     
  29. Sub-Ciro

    Sub-Ciro

    Joined:
    Jan 24, 2015
    Posts:
    5
    i'm trying to open "http://www.google.com" but it shows an Android log that can't open a PDF file called as the URL given, this is weird am i missing something?

    where or how can i define the authority name? i just used the Package Name idk if i'm wrong
     
  30. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    Were you able to solve this? I'm also having problems, my app crashes in my case, when I try opening a URL with a pdf...

    https://www.somewebsite.com/somefile.pdf

    Any ideas?
     
  31. UnetDev

    UnetDev

    Joined:
    Aug 28, 2017
    Posts:
    54
    If your app target level API 24 or more, you need to use FileProvide API, otherwise you get FileUriExposedException, as described here.

    To interact with FileProvider from С# code, you need to know how to interact with AndroidJavaClass and AndroidJavaObject to work with Java libraries (namely android.support.v4). Also you need to know how to declares a content provider component by adding the
    <provider>
    attribute to your AndroidManifest.xml in your project and some more points that are listed here.

    In the answer of this post you can find a solution to the problem, but stumble upon the following troubles:

    1) The android.support.v4 library is no longer available on the path "AndroidSDK/extras/android/support/v4/android-support-v4.jar", since now it is part of another library.

    2) You can find this library in some archives on the Internet, but when you download them you will find that the res folder in Plugins/Android is no longer supported and you need to create a .aar binary file in which you need to put AndroidManifest.xml and android.support.v4 library.

    3) When specifying the attribute
    <provider>
    in AndroidManifest.xml, you need to additionally specify
    <meta-data>
    in which to specify the path, where in your .aar archive the provider_paths.xml file is located, otherwise your application will crash at startup.

    You can try to go this long way yourself, or download my asset, which already has all the necessary code for working with the android.support.v4 library via
    AndroidJavaClass
    and
    AndroidJavaObject
    , and which is also able to rewrite package name in AndroidManifest.xml and providers_paths.xml when you will change it in the project settings. A demo is also included there.

    Link on github: https://github.com/Mihail5412/Unity-Android-Files-Opener
     
    Last edited: Jan 18, 2020
    Yury-Habets likes this.