[英]Get Image from Android gallery to C#
我正在 Unity 中創建一個 android 應用程序(我正在使用 C# 進行編碼),我幾乎完成了,只需要添加一些最終調整以使其有意義。 目前,我無法獲取用戶個人資料的個人資料圖片。 我希望當用戶按下一個按鈕時,他的 android 畫廊會打開,當他選擇一張圖片時,它會顯示在應用程序中,並發送到數據庫。 所以像這樣
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
}
有各種插件可以很好地為您處理。 其中之一是NativeGalery 。 我自己將其用於項目。
我的代碼片段:
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);
});
}
}
UploadNewProfileImage(path);
是我的功能,可將圖像發送到數據庫。 基本上,您應該從路徑中檢索圖像,然后使用流讀取器將其轉換為字節。
不道德的答案很棒。 它適用於安卓和iOS。 你應該使用這個。
"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.