First, add permissions
Permissions added: Player Settings--Other settings--Settings sdcard for write permission. This is a case of packaging in the Unity editor.
If you export to studio, you can modify the manifest file yourself.
Two ways to load the image resources on the sdcard by IO mode
Load the/storage/emulated/0/projectname/image.jpg,
Image = this. Getcomponentinchildren<image> (); Debug.Log ("Io load time: image = this.") getcomponent<image> ========== "+ Image); Debug.Log ("IO Load: Application.datapath" + Application.datapath); application.datapath/data/app/com.putao.ptx.core-1/base.apk Debug.Log ("IO Load: Application.persistentdatapath "+ Application.persistentdatapath); Application.persistentdatapath/storage/emulated/0/android/data/com.putao.ptx.core/files Debug.Log ("IO Load: Ga Meobject.find "+ gameobject.find (" Canvas/avator "). Getcomponent<image> ()); /data/user/0/com.putao.paichallenge/cache/20170524_130527.jpg//Path ======= "/storage/emulated/0/projectn Ame/image.jpg "FileStream FileStream = new FileStream (path, FileMode.Open, FileAccess.Read); Filestream.seek (0, Seekorigin.begin); Create file length buffer byte[] bytes = new Byte[filestream.length]; Read file FileStream.Read (bytes, 0, (int) filestReam. Length); Release file read stream filestream.close (); Filestream.dispose (); FileStream = null; Create Texture int width = 300; int height = 372; Texture2d texture = new Texture2d (width, height); Texture. LoadImage (bytes); Create Sprite Sprite sprite = sprite.create (texture, new Rect (0, 0, texture.width,texture.height), new Vector2 (0. 5f, 0.5f)); Image.sprite = Sprite;
Log can be seen on Android Getexternalfilesdir ("") and unity inside the Application.persistentdatapath is consistent
/storage/emulated/0/android/data/com.putao.ptx.core/files www mode load local picture
Url= "file://" + "/storage/emulated/0/projectname/image.jpg"
Not at all "jar:file://..."
public void Loadbywww (String path) { startcoroutine (doloadbywww (path)); } IEnumerator doloadbywww (string path) { string url = "file://" + "/storage/emulated/0/paichallenge/image.jpg"; Debug.Log ("doloadbywww = = URL ================== " + URL); www w = new www (URL); Yield return w; if (W.isdone) { Sprite sprite = sprite.create (w.texture, New Rect (0, 0, W.texture.width, w.texture.height), New Vector2 (0.5f, 0.5f)); Gameobject.find ("Canvas/avator"). Getcomponent<image> (). Sprite = Sprite; }
Unity reads Android SDcard files