Android Ultimate Plugin Tutorial part 13 – Image Picker and share

Hi Guys and Gals, here we are again and now we will tackle about Image Picker, this is also the new features that is added on Android Ultimate Plugin version 1.3.8 and being said that let’s begin.

First we must  always check our Android Manifest File and make sure we actually have it on this path “Assets/Plugins/Android” and make sure we have the following basic Permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.GET_TASKS"/>

after checking and adding those permissions on your Android Manifest File we can now start coding and do the image picking feature.

First we need to have the instance of ImagePickerPlugin

private ImagePickerPlugin imagePickerPlugin = ImagePickerPlugin.GetInstance();

Then we  decide SetDebug to 1 to see the Debug Toast Messages or 0 to hide them

imagePickerPlugin.SetDebug(0);

We Then Initialize our ImagePickerPlugin

imagePickerPlugin.Init();

After Initialization we need to setup a Callback Listener for ImagePicker Event and pass onGetImageComplete, onGetImageCancel and onGetImageFail methods.

imagePickerPlugin.SetImagePickerCallbackListener(onGetImageComplete,onGetImageCancel,onGetImageFail);

private void onGetImageComplete(string imagePath){
    this.imagePath = imagePath;        
}

private void onGetImageCancel(){        
    //do something here
}

private void onGetImageFail(){
   //do something here
}

After that we also need AndroidUltimatePluginController to access ShareImage Method

private AndroidUltimatePluginController androidUltimatePluginController = AndroidUltimatePluginController.GetInstance();

Our code now will look something like this

    private ImagePickerPlugin imagePickerPlugin;
    private AndroidUltimatePluginController androidUltimatePluginController;
    private string imagePath="";
    public RawImage rawImage;

    // Use this for initialization
    void Start (){
        imagePickerPlugin = ImagePickerPlugin.GetInstance();
        imagePickerPlugin.SetDebug(0);
        imagePickerPlugin.Init();
        imagePickerPlugin.SetImagePickerCallbackListener(onGetImageComplete,onGetImageCancel,onGetImageFail);

        androidUltimatePluginController = AndroidUltimatePluginController.GetInstance();
        androidUltimatePluginController.SetDebug(0);
    }
    private void onGetImageComplete(string imagePath){
        this.imagePath = imagePath;        
    }

    private void onGetImageCancel(){        
        //do something here
    }

    private void onGetImageFail(){
        //do something here
    }

and now to open the gallery and get image we need to call GetImage method

public void GetImage(){
   imagePickerPlugin.GetImage();        
}

Then when it’s complete we will received an Image path this path can now be use to share it on social media applications like Facebook, Twitter or even gmail just call androidUltimatePluginController.ShareImage() and pass “your share title”, “your share message” and image path

private void onGetImageComplete(string imagePath){
     this.imagePath = imagePath;        
}

public void ShareImage(){
    if(!imagePath.Equals("",StringComparison.Ordinal)){
        androidUltimatePluginController.ShareImage("MyPictureSubject","MyPictureSubjectContent",imagePath);            
    }else{
        Debug.Log("[CameraDemo] imagepath is empty");            
    }
}

or if you want you can get this image and load it as texture inside Unity3d using Utils.LoadTexture and pass image path on it

private void LoadImage(){
   //loads texture
   RawImage rawImage.texture = Utils.LoadTexture(imagePath);        
}

Note: saving image or loading image takes some time that’s why we need to set delay before loading or sharing in order to do that we can do something like this

for loading

Invoke("LoadImage",0.3f);

for sharing

Invoke("ShareImage",0.3f);

 

For Reference here’s the code that we used on Image Picker Demo

Note: to make this code run you need Text and RawImage UI just drag them on this code inspector and also don’t forget Button to call Get Image Method and to call Share.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

public class ImagePickerDemo : MonoBehaviour {

    private ImagePickerPlugin imagePickerPlugin;
    private AndroidUltimatePluginController androidUltimatePluginController;

    public Text statusText;
    private string imagePath="";

    public RawImage rawImage;
    public Button shareButton;

    // Use this for initialization
    void Start () {
        imagePickerPlugin = ImagePickerPlugin.GetInstance();
        imagePickerPlugin.SetDebug(0);
        imagePickerPlugin.Init();
        imagePickerPlugin.SetImagePickerCallbackListener(onGetImageComplete,onGetImageCancel,onGetImageFail);


        androidUltimatePluginController = AndroidUltimatePluginController.GetInstance();
        EnableDisableShareButton(false);
    }
    
    public void GetImage(){
        imagePickerPlugin.GetImage();
        EnableDisableShareButton(false);
    }    

    public void ShareImage(){
        if(!imagePath.Equals("",StringComparison.Ordinal)){
            androidUltimatePluginController.ShareImage("MyPictureSubject","MyPictureSubjectContent",imagePath);
            UpdateStatus("Sharing Picture");
        }else{
            Debug.Log("[CameraDemo] imagepath is empty");
            UpdateStatus("can't image path is empty");
        }
    }

    private void UpdateStatus(string status){
        if(statusText!=null){
            statusText.text = String.Format("Status: {0}",status);
        }
    }

    private void DelayLoadImage(){
        //loads texture
        rawImage.texture = AUP.Utils.LoadTexture(imagePath);
        
        UpdateStatus("load image complete");
        EnableDisableShareButton(true);
    }

    private void EnableDisableShareButton(bool val){
        shareButton.interactable = val;
    }
    
    private void LoadImageMessage(){
        UpdateStatus("Loading Image...");
    }

    private void onGetImageComplete(string imagePath){
        this.imagePath = imagePath;

        UpdateStatus("Capture ImageComp lete");
        
        Invoke("LoadImageMessage",0.3f);
        Invoke("DelayLoadImage",0.5f);

        Debug.Log("[CameraDemo] onGetImageComplete imagePath " + imagePath);
    }

    private void onGetImageCancel(){
        UpdateStatus("onGetImageCancel");
    }

    private void onGetImageFail(){
        UpdateStatus("onGetImageFail");
    }
}

 

And that’s all of it , see this is so easy to used and easy to understand, that’s why if I were you? you must purchased our Android Ultimate Plugin now

For more Information

http://www.gigadrillgames.com/android-ultimate-plugin/

For more Tutorials

http://www.gigadrillgames.com/2015/07/26/list-of-tutorials-for-android-ultimate-plugin/

For questions or comments or clarification or any Feed back just email us at gigadrillgames@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.