1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Share images and videos in Android 7 as well as older versions

Discussion in 'Android' started by nir_b, Jun 18, 2017.

  1. nir_b

    nir_b

    Joined:
    Jun 18, 2017
    Posts:
    5
    Hi,

    I am looking for a way to share images and videos through unity. All the examples I saw share fine in Android 6 but do not share in Android 7 due to new security in Android 7

    AndroidJavaException: android.os.FileUriExposedException: ..... exposed beyond app through ClipData.Item.geUri()

    See
    https://developer.android.com/reference/android/os/FileUriExposedException.html
    and also if you know java https://stackoverflow.com/questions...tion-file-storage-emulated-0-test-txt-exposed

    I am not a Java guy.... does someone have an example on how to share images and videos in Android 7 as well as older versions?

    Thanks!
     
  2. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    48
    If you can, just set you target API to 23 and it should work on android 7 too (I have the same problem but I'm having trouble setting the API to 23 due to manifest issues)
     
  3. nir_b

    nir_b

    Joined:
    Jun 18, 2017
    Posts:
    5
    thanks a lot for your answer!

    let me guess.... firebase issues as it needs android:targetSdkVersion="24" ?
    its catch 22
     
  4. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    48
    Unity IAP, but yeah it needs version 24. If setting a lower target API is not an option, try this tutorial, it may inspire you as it explains how to use FileProvider: http://addcomponent.com/android-camera-complete-tutorial/

    By the way what files do you want to be able to share? (what's the use case?)
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    210
  6. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    48
    I have managed to get everything to work with the FileProvider making an Android plugin in Visual Studio with this tutorial: http://addcomponent.com/android-camera-complete-tutorial/
    This tutorial is only for opening the camera, taking a picture and retrieving the picture's path in Unity, but similarly I could add new Fragment classes to share a screenshot and open the gallery (I'll be happy to put the code here if someone needs it).

    Looking a your link yasirkula I'm very relieved to see that taking the screenshot in Unity and then passing the path of the image to the plugin is not so crazy after all! I tried for hours to take the screenshot in the plugin but couldn't get anything but white images, so I used the same technique as they did. Glad to see it's something other people have done too!

    Now everything works in Android 7 while compiling with API 24+ as the target.
     
  7. nir_b

    nir_b

    Joined:
    Jun 18, 2017
    Posts:
    5
    I have tried your plugin on android 7.

    2 issues:

    1) you need to add Assets\Plugins\Android\res\xml\provider_paths.xml
    with
    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    </paths>

    so android:resource="@XML/provider_paths"/> in AndroidManifest.xml wont throw an error

    2) android throws this error after reaching the app you wan to share through:
    ava.lang.IllegalArgumentException: Unable to find configured root for content://com.myapp/devroot/storage/emulated/0/DCIM/175322.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getFileForUri(FileProvider.java:739)
    at android.support.v4.content.FileProvider.getType(FileProvider.java:474)
    at android.content.ContentProvider$Transport.getType(ContentProvider.java:251)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:149)
    at android.os.Binder.execTransact(Binder.java:565)


    hope i helped. hope you can make it work on android 7 :)
     
  8. nir_b

    nir_b

    Joined:
    Jun 18, 2017
    Posts:
    5
    Last edited: Jul 17, 2017 at 4:14 PM
  9. GiyomuGames

    GiyomuGames

    Joined:
    Jan 12, 2015
    Posts:
    48
    I don't think I can nor should. I just followed the tutorial and there are several places where you need to modify variable names and such to match your project, so my plugin wouldn't help at all as you would need to modify it by following the tutorial. Basically the tutorial is the guy sharing his plugin and explaining each step, so it's what exactly what you need.

    If you are stuck at a specific step of the tutorial let me know, but first make sure you follow his steps exactly. If you miss 1 step it won't work.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    210
    I am getting confused by the "android.support.v4.content.FileProvider" lines. I initially couldn't get android.support library to work on Unity so I copy&pasted the source code of android.support.v4.content.FileProvider to
    com.yasirkula.unity.UnitySSContentProvider and used that class instead. It is, therefore, surprising to see the android.support.v4.content.FileProvider on the stacktrace.

    Can you test it on an older device without applying the step 1) you offered?
     
  11. nir_b

    nir_b

    Joined:
    Jun 18, 2017
    Posts:
    5
    without the res dir Unity was not able to compile the apk:
    AndroidManifest.xml:16: error: Error: No resource found that matches the given name (at 'resource' with value '@XML/provider_paths').

    android.support.v4.content.FileProvider - you are right. it was from another AndroidManifest.xml in the project. once i left only your <provider> section it worked!

    thanks a lot for creating this plugin!
     
    yasirkula likes this.