Issue
I'm creating an android app in Unity(I'm coding in C#), I'm nearly done just need to add some final tweaks for it to make sense. Currently I'm having trouble with getting a profile picture for users profile. I would like that when a user presses a button his android gallery opens and when he chooses a picture it is displayed in the app, and sent to database. So something like this
public Button ChangeImage;
public Image ProfilePicture;
void Start()
{
ChangeImage.onClick.AddListener(ChangeImageClicked);
}
void ChangeImageClicked()
{
//here gallery would open and selected image would be returned
//ProiflePicture = ChoosenImage
}
I googled for a solution for about a week now but didn't manage to find it. I read about Intents but C# doesn't recognise them. All help will be greatly apriciated.
Solution
There are various plugins that handle this for you nicely. One of them is NativeGalery. I have used it myself for a project.
A snippet from my code:
public Image LocalProfileImage;
public void ShowMediaPicker()
{
if (Application.isEditor)
{
// Do something else, since the plugin does not work inside the editor
}
else
{
NativeGallery.GetImageFromGallery((path) =>
{
UploadNewProfileImage(path);
Texture2D texture = NativeGallery.LoadImageAtPath(path);
if (texture == null)
{
Debug.Log("Couldn't load texture from " + path);
return;
}
LocalProfileImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
});
}
}
The UploadNewProfileImage(path);
is a function of mine that sends the image to a database. Basically you should retrieve the image from the path and then convert it into bytes using a streamreader.
Answered By - Immorality
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.