Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Very simple example script to use yasirkula’s Native File Picker to load image file from Android device then showing it on UI canvas in Unity

Posted on September 24, 2024September 24, 2024 by Habibie

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

Post Views: 1,061
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme