Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Native File Picker for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Jun 15, 2020.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    At its current state, screen will freeze as you've experienced while copy is in progress.
     
  2. aduong

    aduong

    Joined:
    Aug 11, 2021
    Posts:
    1
    Hey, i'm trying to get a user to pick a directory as I'll require access to multiple images (more than a user would have the patience to select).
    I have tried just getting the user to select one image and use that as a filepath and get the directory information that way but I couldn't actually access the other files. My functionality works when all the files are selected using multiplefiles, just not single.
    Is there a way for the user to pick just a directory folder instead? I've tried using different mime types but it didn't work out.
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  4. IARI

    IARI

    Joined:
    May 8, 2014
    Posts:
    70
    I want to be able to pick jpg and png images on Androd for now. Consequently I use

    Code (CSharp):
    1.     private static readonly string[] allowedFileTypes = new string[] {
    2.         "image/jpg",
    3.         "image/png",
    4.     };
    In Player Settings Write Permission is set to "External" - however, I just want to import.
    On Android In the picker all Images are greyed out, I cannot select anything.

    What could be the problem?
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Can you try changing "image/jpg" to "image/jpeg"?
     
  6. IARI

    IARI

    Joined:
    May 8, 2014
    Posts:
    70
    Thanks, I simply changed it to "image/*", that solved it.
     
    yasirkula likes this.
  7. gerd_3d

    gerd_3d

    Joined:
    Jan 8, 2021
    Posts:
    3
    Hey :)

    We have some issues with the plugin when we use the Unity cloud build service. This results in that the files (e.g fbx, obj) no longer having their original file names but having a ".bin" appended to them.
    This only occurs on Android using IL2CPP build in the Unity cloud. A local build works as expected. Are there perhaps settings we are missing?
     
    Last edited: Sep 15, 2021
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    The issue is that the FBX assets in your Unity project become BIN files for some reason, right? Or is it that the FBX files you pick with this plugin are returned as BIN files?
     
  9. gerd_3d

    gerd_3d

    Joined:
    Jan 8, 2021
    Posts:
    3
    These are the paths/files we receive from the FilePicker as callback. These are different under the circumstances described above. The resulting files are also correct and complete. Only the filename/path has the extension .bin

    Example: /path/to/file/example.fbx.bin
    Expected result: /path/to/file/example.fbx

    This happens only with "no classic media types". Pictures, videos, etc. work as expected.
    It also seems to be independent of the Unity version used. We are using 2020.3.17.

    This is not a big issue for us. It just an unexpected behavior.

    Thanks for your help :)
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    I think this line returns .bin instead of .fbx for FBX files when Cloud Build is used. Thanks for reporting it. I'd like to keep it as is if it doesn't affect your app's functionality.
     
    Last edited: Nov 21, 2021
  11. gerd_3d

    gerd_3d

    Joined:
    Jan 8, 2021
    Posts:
    3
    We developed a workaround for us. Since we work with the file extensions.
     
    yasirkula likes this.
  12. jo_cchcc

    jo_cchcc

    Joined:
    Jul 13, 2021
    Posts:
    2
    On Xiaomi Mi9 (Android 11), 'path' of FilePickedCallback is always null.
    I wrote details to here(github issue), please check it.
     
    yasirkula likes this.
  13. StarArcher

    StarArcher

    Joined:
    May 23, 2020
    Posts:
    12
    I just started using your plugin today and it's working great. Is there a way to get the full path & filename for the file that was Exported... that is, after the user may have changed the directory and renamed the file in the picker? Similar to the "path" that NativeFilePicker.PickFile returns?

    Thank you!
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    To my knowledge, this isn't possible on iOS and, when Storage Access Framework is used (which is when Android 10+), on Android.
     
  15. BinaryBanana

    BinaryBanana

    Joined:
    Mar 17, 2014
    Posts:
    81
    Thank you! You saved me a lot of time :) It worked well on iOS. I didn't try Android yet but I am sure it will work.

    I throw a coin for your coffee, I hope I am not cheap. I might come back once I integrated it on Android :)
    upload_2022-1-6_21-2-58.png
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    Thank you very much, I appreciate your support :)
     
  17. mkirill97

    mkirill97

    Joined:
    Jan 16, 2020
    Posts:
    3
    Hello. I ran into a strange problem.
    Xiaomi Mi A2 lite Android 10. When choosing a photo, the phone gallery closes, and my application restarts and starts over. I implemented all the fixes from the FAQ section.
    Everything works fine on Pixel 3 Android 12. What could be causing this problem?

    My code:
    Code (CSharp):
    1. Texture2D texture = EditAvatarImage.sprite.texture;
    2. string[] fileTypes = new string[] { "image/*" };
    3. NativeFilePicker.Permission permission = NativeFilePicker.PickFile((path) =>
    4.         {
    5.             if (path == null)
    6.                 Debug.Log("Operation cancelled");
    7.             else
    8.                 Debug.Log("Picked file: " + path);
    9.                 _avatarByte = File.ReadAllBytes(path);
    10.                 texture.LoadImage(_avatarByte);
    11.         }, fileTypes);
    12.  
    13.  
    14.         EditAvatarImage.sprite = Sprite.Create(
    15.                 texture,
    16.                 new Rect(0.0f, 0.0f, texture.width, texture.height),
    17.                 new Vector2(0.5f, 0.5f), 100.0f);
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    My guess is RAM shortage. Game uses all of its reserved RAM and restarts as a result. This is not uncommon when using plugins like this on old devices. You can add the plugin to a fresh new project and see if it restarts on Xiaomi, as well.
     
  19. DeveloperGT

    DeveloperGT

    Joined:
    Mar 2, 2020
    Posts:
    6
    Hi,

    I am trying to use Native File Picker for Android for oculus quest 2 device. Is the Native File Picker applicable for VR? I see only loading symbol inside the device.
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    I remember someone else having the same issue. I don't think this plugin works in VR. For VR, you can use SimpleFileBrowser.
     
  21. DeveloperGT

    DeveloperGT

    Joined:
    Mar 2, 2020
    Posts:
    6
    Okay Thanks for your quick response.
     
  22. Paul-Bones

    Paul-Bones

    Joined:
    Apr 4, 2016
    Posts:
    3
    Hello @yasirkula thanks for your great work!
    I just started to use your plugin, but I having some troubles to use correctly the export function, I have a pdf file on my server, I download it to persistentDataPath and use the export function of the plugin to give the user the option to save that file wherever he wants.
    What I'm doing right now is just saving that pdf path after downloading it and just calling this:
    NativeFilePicker.ExportFile(pdfPath, (success) => debugtext.text += ("File exported: " + success));

    That opens a file browser window with a .pdf extension automatically putted in the name field, I put a name to it and click save, it saves the file but with 0 bytes, it seems to be just a .pdf file with no data, when I try to open it using the file explorer it doesn't open.

    There is something that I'm doing wrong by sure :D
    What I'm doing wrong?

    Thanks in advance
     
  23. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @Paul-Bones Hi! Could you check Logcat to see if there are any error messages there?
     
  24. epernigo

    epernigo

    Joined:
    Sep 24, 2019
    Posts:
    11
    Hi! Does Native File Picker need READ_EXTERNAL_STORAGE permission on Android?
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @epernigo Yes, that permission is asked to be safe.
     
    epernigo likes this.
  26. antonioritucci336

    antonioritucci336

    Joined:
    Dec 2, 2021
    Posts:
    3
    Hi @yasirkula

    How can I ask for .fbx files?

    I'm trying to use the file picker to open .fbx file on Android phone (BV9000 Pro).
    Following your example I did like this:
    Code (CSharp):
    1. string fbxFileType = NativeFilePicker.ConvertExtensionToFileType("fbx");
    2.  
    3.         NativeFilePicker.Permission permission = NativeFilePicker.PickFile((path) =>
    4.         {
    5.             if (path == null)
    6.                 Debug.Log("Operation cancelled");
    7.             else
    8.                 Debug.Log("Picked file: " + path);
    9.         }, new string[] { fbxFileType }
    10.         );
    Now it shows something like
    "Unable to Find Application to Perform this Action"
    ( I don't know if exactly like this because it is showing me in Italian )
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @antonioritucci336 Your code looks perfectly fine to me, that's kinda strange. Can you replace fbxFileType with
    "application/octet-stream"
    and see if it prompts the same error? If not, can you log fbxFileType to logcat and see what it outputs?
     
  28. antonioritucci336

    antonioritucci336

    Joined:
    Dec 2, 2021
    Posts:
    3
    @yasirkula I tried with "application/octet-stream" I menaged to select .fbx and other octet-stream files
    Using this
    Code (CSharp):
    1. string fbxFileType = NativeFilePicker.ConvertExtensionToFileType("fbx");
    2. Debug.Log("fbxFileType: " + fbxFileType);
    It gives me this log:

    I/Unity: fbxFileType:

    Guess the problem "Unable to Find Application to Perform this Action" is because it's using as
    Code (CSharp):
    1. fileTypes = new string[] { "" };
    This works fine to look for .png files:
    Code (CSharp):
    1. string fbxFileType = NativeFilePicker.ConvertExtensionToFileType("fbx");
    2.         string pngFileType = NativeFilePicker.ConvertExtensionToFileType("png");
    3.         Debug.Log("fbxFileType: " + fbxFileType);
    4.  
    5.         NativeFilePicker.Permission permission = NativeFilePicker.PickFile((path) =>
    6.         {
    7.             if (path == null)
    8.                 Debug.Log("Operation cancelled");
    9.             else
    10.                 Debug.Log("Picked file: " + path);
    11.         }, new string[] { fbxFileType, pngFileType }
    12.         );
    With:
    Code (CSharp):
    1. new string[] { "application/octet-stream" }
    I can select .fbx on Android, how about IOS?

    Thank you for your precious work :)
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @antonioritucci336 I thought NativeFilePicker.ConvertExtensionToFileType returned "application/octet-stream" as fallback value but I've realized that indeed it returns an empty string. I'd recommend you to set fbxFileType to "application/octet-stream" if it's null or empty (string.IsNullOrEmpty). Then continue passing that variable to PickFile function. I don't expect you to hit any obstacles on iOS but if you do, please add fbx to "Window-NativeFilePicker Custom Types" and it should work.
     
    antonioritucci336 likes this.
  30. CosmicCricket

    CosmicCricket

    Joined:
    Jul 4, 2018
    Posts:
    3
    Hello, I am new to iOS development.

    Right now I am using this asset to locate the filepath of one or more PNGs stored in the iOS Files app so that I can convert them into Texture2Ds via System.IO.File.ReadAllBytes.

    This works perfectly fine in the moment, but I am also trying to create a "recent files" section where the user can access recent PNG selections without going through the file picker interface. When I try to save the filepaths of PNGs locally, and access them via System.IO.File.ReadAllBytes, I always end up with System.IO.FileNotFoundExceptions.

    I am accounting for the fact that the app GUID in the app's local filepath is always changing on iOS by retrieving it at startup.

    I am assuming I am running afoul of some restriction that Apple places on filesystem access, so any advice on how to proceed with this "recent files" functionality would be appreciated.
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @CosmicCricket Hi! iOS doesn't return raw file paths (it returns NSURLs instead) so I copy the selected file to temporaryCachePath using native API and return that path. If you were saving that path, I'd actually expect it to load correctly the next time but perhaps the OS clears that content's directory too frequently. If you wish, you can copy the returned file to persistentDataPath with a unique name and save that path instead.
     
  32. thelocationlabdeveloper

    thelocationlabdeveloper

    Joined:
    Sep 8, 2020
    Posts:
    18
    Is this plugin allow to open pdf file on iOS? As i am unable to find the alternate of Application.OpenURL on ios platform.
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    NativeFilePicker can pick PDF files from the device (and return their file path) but it can't open them. You need to open the returned PDF files with another solution.
     
  34. CosmicCricket

    CosmicCricket

    Joined:
    Jul 4, 2018
    Posts:
    3
    Then, would it be possible to use either these NSURLs or filepaths extracted from NSURLs with Unity C# file I/O in order to access my files directly?
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    It isn't possible to extract filepaths from NSURL. As I said, one thing you can do is save the returned filepath to persistentDataPath with a unique name. If you must work with NSURLs, you'll need to write native Objective-C code for it. I haven't researched how to save NSURLs or how to retain access to them after the app is restarted so I can't help much with this feature.
     
  36. AmienNguyen

    AmienNguyen

    Joined:
    Jul 21, 2020
    Posts:
    2
    Hi @yasirkula,
    I cannot select a file (file appear greyed out/ disable all files) in iOS, although I already set the list extension:
    new string[] { ".fbx", ".obj", ".glb", ".zip" }
    Is there anything I have missed to config and how to add custom file extensions to the "Window-NativeFilePicker Custom Types" asset. I have no idea set extension .fbx into NativeFilePicker Custom Types with fields such as: identifier, description, Is Exported...).
    Please help me, thanks!
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  38. Jarpitskij

    Jarpitskij

    Joined:
    Jun 20, 2020
    Posts:
    5
    Hi @yasirkula,

    I tried automated setup and manual setup, yet all files in iCloud drive are greyed out. I tried with and without custom file types. Xcode version 14.0.1, Unity version 2021.3.11f1 and iOs 16.0.2. Native share for Android & iOs is version 1.4.7. I am just trying to pick file. I use Native Share for Android & iOs -plugin also, could it bother this one?

    Any advice?

    Thanks!
     
  39. Jarpitskij

    Jarpitskij

    Joined:
    Jun 20, 2020
    Posts:
    5


    I am taking back my previous comment. I got it working with pdf example. Now i just need to figure out the custom type. :)
    Thanks.
     
    yasirkula likes this.
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  41. Jarpitskij

    Jarpitskij

    Joined:
    Jun 20, 2020
    Posts:
    5
    yasirkula likes this.
  42. valkirin2000

    valkirin2000

    Joined:
    Sep 27, 2022
    Posts:
    4
    Hello, @yasirkula ! Could you tell me if I can use Native File Picker for importing .mp3 files from android mobile phone into my application?
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @valkirin2000 Hi, yes I'd expect this to work. You can replace pdf with mp3 in the online example code.
     
  44. santa992

    santa992

    Joined:
    Mar 3, 2017
    Posts:
    10
    Hi @yasirkula
    I have this code

    Code (CSharp):
    1.     private void ImportAudio()
    2.     {
    3.         Debug.Log("Import audio");
    4. #if UNITY_ANDROID
    5.             // Use MIMEs on Android
    6.             string[] fileTypes = new string[] { "audio/*"};
    7. #else
    8.         // Use UTIs on iOS
    9.         string[] fileTypes = new string[] { "public.audio" };
    10. #endif
    11.  
    12.         // Pick image(s) and/or video(s)
    13.         NativeFilePicker.Permission permission = NativeFilePicker.PickMultipleFiles((paths) =>
    14.         {
    15.             ManageBuzzers.Instance.SetText("ImportAudio");
    16.             if (paths == null)
    17.                 Debug.Log("Operation cancelled");
    18.             else
    19.             {
    20.                 ManageBuzzers.Instance.SetText("ImportAudio else");
    21.                 for (int i = 0; i < paths.Length; i++)
    22.                     Debug.Log("Picked file: " + paths[i]);
    23.                 FileInfo f = new FileInfo(paths[0]);
    24.                 DirectoryInfo d = f.Directory;
    25.                
    26.                 PlayerPrefs.SetString(FileMenager.FOLDER_KEY, d.FullName);
    27.                 ManageBuzzers.Instance.SetText("d.FullName:" + d.FullName);
    28.                 FileMenager.Instance.GetSongs();
    29.             }
    30.         }, fileTypes);
    31.  
    32.         Debug.Log("Permission result: " + permission);
    33.     }
    On Ios work fine, but on mac sometimes it work and sometimes come only to ManageBuzzers.Instance.SetText("ImportAudio");
    So I assume that sometimes paths==null
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    The plugin is intended to work on Android & iOS only, so if it sometimes works on Mac, that's unintended :) I don't know about Mac plugin ecosystem unfortunately.
     
  46. DeveloperJake

    DeveloperJake

    Joined:
    May 25, 2020
    Posts:
    13
    This does nothing in the built game in unity on Mac. Is there any way to make this work in build?
     
    nilsdr likes this.
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
    @DeveloperJake Hi! The plugin is intended to work on Android & iOS only, so Mac isn't a supported platform.
     
  48. Atair_

    Atair_

    Joined:
    May 30, 2022
    Posts:
    3
    I am using filepicker on two projects on iOS.. when i pick a file it always starts from the directory of the first project (a folder on my phone i was using to import data).
    Is there is a setting? now both projects default to the same starting directory - is there a way to change that? Best would be to start in the 'Files' App 'Browse' panel.. or to remember the last opened location on each app?
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,865
  50. Atair_

    Atair_

    Joined:
    May 30, 2022
    Posts:
    3
    Thanks! - I asked chatgpt to help me out:
    Code (CSharp):
    1. NSURL *iCloudDriveURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    2.     iCloudDriveURL = [iCloudDriveURL URLByAppendingPathComponent:@"Documents"];
    3.     filePicker.directoryURL = iCloudDriveURL;
    That worked, but only after i deleted the old folder (and emptied the trash) - also all other ios Apps (e.g. mail attachement) defaulted to this one cursed folder - even when it was in the trash. So not sure what fixed it in the end..
     
    yasirkula likes this.