• 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 /
  • Help Room /
avatar image
0
Question by dakotapearl · Jun 25, 2017 at 02:20 PM · androidimageandroidjavaobjectgalleryintent

Pick image from android gallery

So this has been asked before but there was no answer except for go buy someones asset, which is unacceptable for me because I need to know how to do it.

I'm working entirely from c# using AndroidJavaClasses, AndroidJavaObjects and AndroidJavaProxies. My current code is below with my three failed solutions.

Anyone got any ideas??

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ImagePicker : MonoBehaviour {
 
     class OnActivityResultCallback : AndroidJavaProxy {
         public Text textbox;
 
         public OnActivityResultCallback() : base("android.app.Activity") {
         }
 
         protected void onActivityResult(int requestCode, int resultCode, AndroidJavaObject intent) {
             textbox.text += "onActivityResult: " + requestCode;
         }
 
     }
 
     public Text textbox;
 
     public void RequestImageFromGallery() {
 
         AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
         AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
 
         intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_GET_CONTENT"));
         intentObject.Call<AndroidJavaObject>("setType", "image/*");
 
         OnActivityResultCallback callback = new OnActivityResultCallback();
         callback.textbox = textbox;
 
         // Solution 1, use the Unity current activity, problem: no onActivityResult to return result to
         AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
         AndroidJavaObject UnityActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
         UnityActivity.Call("startActivityForResult", intentObject, 1);
 
         // Solution 2, use a AndroidJavaProxy with onActivityResult implemented, problem: there is no activity instance in a proxy and so gives an error about application threads being null
         callback.Invoke("startActivityForResult", new object[] { intentObject, 1 });
 
         // Solution 3, use java reflection from c# to call the startActivityForResult method with the callback as the "this" object, problem: I get an error when trying to get the getMethod method and I'm not sure it'll work anyway
         // class intClass = java.lang.Integer.class; (ish)
         AndroidJavaClass intClass = new AndroidJavaClass("java.lang.Integer");
         // java.lang.Class jclass = obj.getClass();
         // java.lang.reflect.Method method = jclass.getMethod(methodName, param1.class, param2.class, ..);
         AndroidJavaClass jclass = UnityActivity.Call<AndroidJavaClass>("getClass");
         AndroidJavaObject method = jclass.Call<AndroidJavaObject>("getMethod", "startActivityForResult", intentClass, intClass);
         // method.invoke(obj, arg1, arg2,...);
         method.Call("invoke", callback, intentObject, 1);
 
     }
 
 }
 
Comment
Add comment · Show 1
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 DeveshPandey · Dec 18, 2017 at 05:36 PM 0
Share

Here is a nice plugin to pick image and video from gallery in Android and iOS.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Indice_dev · Sep 13, 2017 at 08:21 PM

This link might help you. [link text]Link[1]

[1]: http://addcomponent.com/android-native-plugin-unity/

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
avatar image
0

Answer by SmileSoft4849 · Apr 23, 2018 at 06:23 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 · Show 1 · 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 agha502 · Jul 11, 2019 at 06:37 AM 1
Share

and its paid Sir

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

175 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 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 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 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 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 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 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

Android getUuidForPath of shared/external storage volumes 0 Answers

Text and Image fill lerp issues - Android 0 Answers

Why is USB Storage being detected only in some android devices and not all? 2 Answers

[BUG] AndroidJavaObject getStringArrayExtra() crash 1 Answer

How to get intent Data for Unity to use? 6 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