Unity reads Android SDcard file

1. Add permissions

Permission added: Player settings -- Other settings -- write permission setting Sdcard. This is the case when it is packaged in the Unity editor.

If you export to studio, you can modify the Manifest file by yourself.

 

Two, two ways

IO method to load image resources on sdcard

loaded /storage/emulated/0/ProjectName/image.jpg,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
image =this.GetComponentInChildren<Image>(); 
   
      Debug.Log("IO loading 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 loading: Application.persistentDataPath " + Application.persistentDataPath); 
      // Application.persistentDataPath /storage/emulated/0/Android/data/com.putao.ptx.core/files  
   
   
      Debug.Log("IO Loading: 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); 
      //create file length buffer 
      byte[] bytes =new byte[fileStream.Length]; 
      //read file 
      fileStream.Read(bytes, 0, (int)fileStream.Length); 
      // release the 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; 

It can be seen from the log that getExternalFilesDir( "" ) on android is consistent with Application.persistentDataPath in Unity

   /storage/emulated/0/Android/data/com.putao.ptx.core/files
 

Load local pictures via WWW   

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

Not adding "jar:file://..." at all

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 In; 
   
        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; 
   
        

  

Related: Unity reads Android SDcard file