Unity讀取Android SDcard檔案

來源:互聯網
上載者:User

標籤:this   canvas   sprite   path   bytes   div   cto   匯出   getc   

一、添加許可權

許可權添加 :Player settings -- Other settings -- write permission的設定 Sdcard。這個是在Unity編輯器裡打包的情況。

如果匯出到studio 裡面的話,可自行修改Manifest檔案。

 

二、兩種方式IO方式 載入sdcard上的圖片資源

載入的  /storage/emulated/0/ProjectName/image.jpg,

image = this.GetComponentInChildren<Image>();          Debug.Log("IO載入用時: image = this.GetComponent<Image> ==========  " + image);          Debug.Log("IO載入:  Application.dataPath "  + Application.dataPath );        // Application.dataPath /data/app/com.putao.ptx.core-1/base.apk          Debug.Log("IO載入:  Application.persistentDataPath " + Application.persistentDataPath);        // Application.persistentDataPath /storage/emulated/0/Android/data/com.putao.ptx.core/files             Debug.Log("IO載入:GameObject.Find" + GameObject.Find("Canvas/Avator").GetComponent<Image>());           // /data/user/0/com.putao.paichallenge/cache/20170524_130527.jpg            // path =======  "/storage/emulated/0/ProjectName/image.jpg"        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);        fileStream.Seek(0, SeekOrigin.Begin);        //建立檔案長度緩衝區        byte[] bytes = new byte[fileStream.Length];        //讀取檔案        fileStream.Read(bytes, 0, (int)fileStream.Length);        //釋放檔案讀取流        fileStream.Close();        fileStream.Dispose();        fileStream = null;          //建立Texture        int width = 300;        int height = 372;        Texture2D texture = new Texture2D(width, height);        texture.LoadImage(bytes);            //建立Sprite             Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width,texture.height), new Vector2(0.5f, 0.5f));          image.sprite = sprite;  

log上可以看出來  android上 getExternalFilesDir("") 和 Unity裡面的Application.persistentDataPath是一致的

   /storage/emulated/0/Android/data/com.putao.ptx.core/files WWW方式載入本地圖片   

url=  "file://"+ "/storage/emulated/0/ProjectName/image.jpg"

根本不是加"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讀取Android SDcard檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.