Unity reads Android SDcard files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.