Search Unity

Step-by-step instructions on how to get a picture from photo gallery.

Discussion in 'Android' started by rawegames, Jan 8, 2017.

  1. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    ANDROID

    I want to pick image from the android picture gallery.

    I have googled for over 4 hours, and have not found any free, working method to pick an image from the android picture gallery.

    These are the best options I have:
    https://www.assetstore.unity3d.com/en/#!/content/28597
    https://www.assetstore.unity3d.com/en/#!/content/9926

    However, they are paid software from the Unity Asset store.

    I can not afford paid assets.

    I have also found this alleged API:
    https://github.com/chchu/unity-pick-image-for-android

    However, it does not have any documentation, and when I put the plugins into my unity project and install it on an android phone, it fails to run.

    May I have clear, step-by-step instructions on how to get a picture from the android picture gallery.
    Here are the details of what I need:
    • Freeware only
    • Detailed instructions, what libraries or plugins are needed? Where to download them?
    • What functions to call to initialize? What functions to call to open the android gallery?
    • Retrieve the chosen picture in the form of Texture2D or Sprite.
    It will help me greatly, and hopefully, others who's finding a solution too.
     
    Last edited: Jan 8, 2017
  2. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,874
    You can use System.IO class to list images from specific directory and select them.
    For learning IO class, you can refer to .Net Framework forums and tutorial pages. It's very helpful in a lot of cases.
     
  3. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    For the past 1 hour, I have been searching for what you said, but have not found any leads to a solution.
    I have a very simple request:
    I want Android's default photo gallery to open up, where the user can select a photo.
    I want the selected photo to be assigned to "chosenPhoto".
    Code (csharp):
    1.  
    2. Sprite chosenPhoto;
    3.  
    May I have step-by-step intructions here?
    I will help me + likely many other people.
     
    Shani_T8 and TheBelge07 like this.
  4. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.IO;
    5.  
    6. public class ImagePicker : MonoBehaviour {
    7.  
    8.     [SerializeField] InputField inf;
    9.     [SerializeField] Reporter rep;
    10.  
    11.     public void OnClickDir () {
    12.         rep.clear();
    13.         PrintFolders(inf.text);
    14.     }
    15.     void PrintFolders(string path){
    16.         foreach(string x in Directory.GetDirectories(path)){
    17.             Debug.Log(x);
    18.         }
    19.     }
    20.     public void OnClickFil () {
    21.         rep.clear();
    22.         PrintFolders(inf.text);
    23.     }
    24.     void PrintFiles(string path){
    25.         foreach(string x in Directory.GetFiles(path)){
    26.             Debug.Log(x);
    27.         }
    28.     }
    29. }
    30.  
    This is the closest I've been to. It's on an empty project, with "Reporter" from Log Viewer, so that I can see the debug logs on an android device.

    The methods OnClickDir, and OnClickFil, are functions that will be called by buttons on click. OnClickDir is called by a button "View Directories", and OnClickFil is called by a button "View Files".

    The InputField "inf" is an InputField where I can enter the path I would like to check out.

    I built the project into an APK, then installed it on my android phone.
    I took a photograph using the camera in my phone, and used the built-in "My Files" app to find the location of the taken photograph.
    I found out that it was in the "/sdcard/DCIM/Camera/P_20170113_085946.jpg".

    Then, I opened up my app to view the directory with path "/sdcard/DCIM/Camera/". The directory showed up as empty.

    Then, I went to the directory using the built-in "My Files" app, and then created an empty folder, and called it "qwert".

    Then, I opened up my app to view the directory with path "/sdcard/DCIM/Camera/". Now, an empty folder "qwert" appears. This confirms that I'm viewing the correct directory. However, I can't see any photographs thru my app.

    Why is this so?

    I don't notice any difference between OnClickDir, vs OnClickFil. What's the difference?

    EDIT: OOPS! I just saw that OnClickFil calls PrintFolders, not PrintFiles. Hold on while I re-test this program.
     
  5. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    I managed to list the .jpg paths by using PrintFiles. However, what if the image isn't in the "/sdcard/DCIM/Camera/" directory? Isn't there an android built-in photo selector API?
     
  6. Lokesh_Sivaprakasam

    Lokesh_Sivaprakasam

    Joined:
    Aug 2, 2017
    Posts:
    3
    I am also struggling for two days to load device gallery image as a material diffuse image and appling on plane in runtime.
     
    NMDhorajiya likes this.
  7. NMDhorajiya

    NMDhorajiya

    Joined:
    Apr 16, 2018
    Posts:
    1
    Did you Got Any solution..
    Actually we are also doing same. In may case it causes problem in low space memory(1GB RAM). In low space device it does not load successfully and just restarts app.
     
  8. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
  9. chayan_b

    chayan_b

    Joined:
    Jun 21, 2019
    Posts:
    2
    Have you got complete solution? If you got it then please share.
     
  10. Dark_horse599

    Dark_horse599

    Joined:
    Aug 9, 2019
    Posts:
    1
  11. abhishek21296

    abhishek21296

    Joined:
    Jun 17, 2020
    Posts:
    4
    https://github.com/yasirkula/UnityNativeGallery
    use this to implement the gallery functionality, works flawlessly for me. the readme has instructions for importing it. though I'm still searching for an extended version of this which gives an option to open camera as well.
     
  12. maazshaikh1711

    maazshaikh1711

    Joined:
    Jun 1, 2021
    Posts:
    5
    Been holding on just kidding, have you got a solution for it? I'm not getting any solution for this...
    Please let me know if you created your solution.
     
  13. maazshaikh1711

    maazshaikh1711

    Joined:
    Jun 1, 2021
    Posts:
    5
  14. maazshaikh1711

    maazshaikh1711

    Joined:
    Jun 1, 2021
    Posts:
    5
    I tried it but it does nothing in my case, don't know why :(
     
  15. maazshaikh1711

    maazshaikh1711

    Joined:
    Jun 1, 2021
    Posts:
    5
    Have you got any solution for it?
     
  16. ozalpm

    ozalpm

    Joined:
    Jun 8, 2020
    Posts:
    3