Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rigor55 · Sep 15, 2013 at 04:04 PM · androidrendererpathselectgallery

Open Gallery Android

hi, I use this code

 function OnGUI(){
     if(GUI.Button(Rect(0,0,100,30),"Resim")){
         var path = EditorUtility.OpenFilePanel(
                     "Overwrite with jpg",
                     "",
                     "jpg");
         var resim = WWW("file:///" + path);
         renderer.material.mainTexture=resim.texture;
     }
 }

in windows. But I want to open gallery in android phone. This code not play in android. What should I do for open gallery in android and select a jpg and example: a plane renderer.material.mainTexture= my select texture. Can you help me, plz.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhapsodyv · Jan 25, 2014 at 12:12 AM 0
Share

did you solve it?!

avatar image DeveshPandey · Jun 30, 2014 at 04:03 PM 0
Share

You have to write java plugin for android to open gallery or pick image from gallery.

you have to use following code in java and call it from unity.

Intent intent = new Intent();

intent.setType("image/*");

intent.setAction(Intent.ACTION_VIEW);

startActivity(intent);

for more detail you can write me on devesh.pandey19@gmail.com

http://u3d.as/content/devesh-pandey/capture-and-save/86U

avatar image DeveshPandey · Feb 11, 2015 at 03:20 PM 0
Share

Browse image,video and contact in Android

https://www.assetstore.unity3d.com/en/#!/content/28597

Open gallery or another android app from unity

http://u3d.as/content/devesh-pandey/app-launcher/8$$anonymous$$H

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Guy-Corbett · Feb 09, 2016 at 10:02 AM

Hi there,

I know I'm a bit late to the party on this but I came up with my own solution and thought I would post it in case you are still trying to do this or for anyone else who finds this page.

I have written a function which gets the file paths of all the images in the android gallery. Once you have them you can load them as you would any other image on disk. My function looks like this:

     private List<string> GetAllGalleryImagePaths()
     {
         List<string> results = new List<string>();
         HashSet<string> allowedExtesions = new HashSet<string>() { ".png", ".jpg",  ".jpeg"  };
 
         try
         {
             AndroidJavaClass mediaClass = new AndroidJavaClass("android.provider.MediaStore$Images$Media");
 
             // Set the tags for the data we want about each image.  This should really be done by calling; 
             //string dataTag = mediaClass.GetStatic<string>("DATA");
             // but I couldn't get that to work...
             
             const string dataTag = "_data";
 
             string[] projection = new string[] { dataTag };
             AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
             AndroidJavaObject currentActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
 
             string[] urisToSearch = new string[] { "EXTERNAL_CONTENT_URI", "INTERNAL_CONTENT_URI" };
             foreach (string uriToSearch in urisToSearch)
             {
                 AndroidJavaObject externalUri = mediaClass.GetStatic<AndroidJavaObject>(uriToSearch);
                 AndroidJavaObject finder = currentActivity.Call<AndroidJavaObject>("managedQuery", externalUri, projection, null, null, null);
                 bool foundOne = finder.Call<bool>("moveToFirst");
                 while (foundOne)
                 {
                     int dataIndex = finder.Call<int>("getColumnIndex", dataTag);
                     string data = finder.Call<string>("getString", dataIndex);
                     if (allowedExtesions.Contains(Path.GetExtension(data).ToLower()))
                     {
                         string path = @"file:///" + data;
                         results.Add(path);
                     }
 
                     foundOne = finder.Call<bool>("moveToNext");
                 }
             }
         }
         catch (System.Exception e)
         {
             // do something with error...
         }
 
         return results;
     }

And you can use it like this:

     [SerializeField]
     private RawImage m_image;
 
     public void SetImage()
     {
         List<string> galleryImages = GetAllGalleryImagePaths();
         Texture2D t = new Texture2D(2, 2);
         (new WWW(galleryImages[0])).LoadImageIntoTexture(t);
         m_image.texture = t;
     }

Hope that helps!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hassansohail00 · May 04, 2017 at 03:37 PM 0
Share

how can i use it?

avatar image masteryder · Jun 26, 2017 at 11:48 AM 0
Share

Your solution works pretty well, although I really have no idea of what's going on in it, $$anonymous$$d to explain a bit?

avatar image italomarques · Oct 14, 2017 at 08:03 PM 0
Share

need permision?

avatar image italomarques · Oct 14, 2017 at 08:18 PM 0
Share

need manifest permission to READ_EXTERNAL_STORAGE ???

avatar image italomarques · Oct 14, 2017 at 09:46 PM 0
Share

i finde the solution...need to put this line in Android$$anonymous$$anifest.xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Show more comments
avatar image
3

Answer by ShawnFeatherly · May 22, 2015 at 11:13 PM

With some help from DeveshPandey's comment snippet, looking around, and http://www.daniel4d.com/blog/sharing-image-unity-android/ I created this method that works right inside a unity script file:

 /// <summary>
 /// based off 2 lines of Java code found at at http://stackoverflow.com/questions/18416122/open-gallery-app-in-androi
 ///      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
 ///      startActivity(intent); 
 /// expanded the 1st line to these 3:
 ///      Intent intent = new Intent();
 ///      intent.setAction(Intent.ACTION_VIEW);
 ///      intent.setData(Uri.parse("content://media/internal/images/media"));
 /// </summary>
 public void OpenAndroidGallery()
 {
     #region [ Intent intent = new Intent(); ]
     //instantiate the class Intent
     AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");

     //instantiate the object Intent
     AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
     #endregion [ Intent intent = new Intent(); ]

     #region [ intent.setAction(Intent.ACTION_VIEW); ]
     //call setAction setting ACTION_SEND as parameter
     intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_VIEW"));
     #endregion [ intent.setAction(Intent.ACTION_VIEW); ]

     #region [ intent.setData(Uri.parse("content://media/internal/images/media")); ]
     //instantiate the class Uri
     AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");

     //instantiate the object Uri with the parse of the url's file
     AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "content://media/internal/images/media");

     //call putExtra with the uri object of the file
     intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
     #endregion [ intent.setData(Uri.parse("content://media/internal/images/media")); ]

     //set the type of file
     intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");

     #region [ startActivity(intent); ]
     //instantiate the class UnityPlayer
     AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

     //instantiate the object currentActivity
     AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

     //call the activity with our Intent
     currentActivity.Call("startActivity", intentObject);
     #endregion [ startActivity(intent); ]
 }

I don't know what the "content://media/internal/images/media" uri is for. Changing the uri to whatever seems to do the same thing. Understanding that uri would be nice, but I was unable to find information on it.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vanshika1012 · Sep 04, 2015 at 11:24 AM 0
Share

Hi,

Thanks for valuable code. Can you please tell me how to choose a particular file. I have successfully open the gallary but not able to choose image.

Thanks in Advance.

avatar image DeveshPandey · Nov 21, 2016 at 05:45 PM 0
Share

Nice plugin is here for you

http://unitydevelopers.blogspot.in/2015/05/image-video-and-contact-picker-image.html

avatar image Jnth · Jul 21, 2017 at 02:09 PM 0
Share

Hi,

It works pretty weel ! Anyone knows how to select a picture ? I mean I would like to select a picture from gallery and then set it as a texture ... I heard about plugin , is it really necessary ?

avatar image nawash Jnth · Jul 27, 2018 at 02:59 PM 0
Share

Hi Jnth, Have you found a solution to select a picture with this great code ?

Thanks

avatar image S0nster · Apr 11, 2020 at 11:32 AM 0
Share

Still works perfectly. $$anonymous$$uch appreciated.

avatar image
0

Answer by SmileSoft4849 · Apr 23, 2018 at 06:27 AM

I have created "Android Native Gallery Item Picker" for this task. It can pick single or multiple image and video from your android phone or android gallery.

Click here for Android Native Gallery Item Picker

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save Gif to Photo Gallery from Persistent Path - iOS Android 1 Answer

Download Image to a new album (Android) 0 Answers

Save a Texture2D for android 1 Answer

Apply texture to object from device gallery 0 Answers

How to get path sd card in android ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges