This free asset from yasurkula is very nice, it allows you to pick image file from your Android device easily, it also deals with app permission so you can just call a single function to request permission, so easy!
Here is the link to the Native File Picker in Asset Store: https://assetstore.unity.com/packages/tools/integration/native-file-picker-for-android-ios-173238
In below is a simple script that you can use to pick an image file and then show it on a UI image component in Unity Canvas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class NfpExample : MonoBehaviour
{
private string pdfFileType;
public Image img;
void Start()
{
RequestPermissionAsynchronously(false);
}
void Update()
{
}
private async void RequestPermissionAsynchronously( bool readPermissionOnly = false )
{
NativeFilePicker.Permission permission = await NativeFilePicker.RequestPermissionAsync( readPermissionOnly );
Debug.Log( "Permission result: " + permission );
}
public void OpenImageFile(){
#if UNITY_ANDROID
// Use MIMEs on Android
string[] fileTypes = new string[] { "image/*" };
#else
// Use UTIs on iOS
string[] fileTypes = new string[] { "public.image" };
#endif
// Pick an image file
NativeFilePicker.Permission permission = NativeFilePicker.PickFile( ( path ) =>
{
if( path == null ){
Debug.Log( "Operation cancelled" );
}
else{
Debug.Log( "Picked file: " + path );
byte[] bytes = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(1, 1);
texture.filterMode = FilterMode.Trilinear;
texture.LoadImage(bytes);
Sprite sprite = Sprite.Create(texture, new Rect(0,0,texture.width, texture.height), new Vector2(0.5f,0.0f), 1.0f);
img.sprite = sprite;
}
}, fileTypes );
Debug.Log( "Permission result: " + permission );
}
}
But there is a problem: The imported image in Android device is displayed rotated. Then we need to figure out how to rotate it so it can be displayed correctly.
I did not try it, but you can see some online forums, for example this one: https://discussions.unity.com/t/rotate-the-contents-of-a-texture/136686