Unity製のアプリ(PC,Mac&Linux Standaloneの中でwindows向けのビルド)で、ユーザからPC内の画像を取得する方法が知りたいです。
その際、ユーザが選択した画像のファイルパスを取得することはできたのですが、それをbyte[]に変換できません。
具体的には、readPngFileにユーザが取得したfile:/Users/xxxxxxxxxx/TestPOSTAPI/Assets/Resources/dummy01.pngを入れたところ、
DirectoryNotFoundException: Could not find a part of the path "/Users/xxxxxxxxxx/TestPOSTAPI/file:/Users/xxxxxxxxxx/TestPOSTAPI/Assets/Resources/dummy01.png"
というエラーが出ました。
csharp
1 2using UnityEngine; 3using System.IO; 4 5public class TestScript : MonoBehaviour { 6 void Start(){ 7 string path = "file:/Users/xxxxxxxxxxxxxx/TestPOSTAPI/Assets/Resources/dummy01.png"; 8 byte[] body = readPngFile(path); 9 } 10 11 byte[] readPngFile(string path) { 12 using (FileStream fileStream = new FileStream (path, FileMode.Open, FileAccess.Read)) { 13 BinaryReader bin = new BinaryReader (fileStream); 14 byte[] values = bin.ReadBytes ((int)bin.BaseStream.Length); 15 bin.Close (); 16 return values; 17 } 18 } 19}
絶対パスにする方法として@をつければ出来るものもあったので、試しましたが同様のエラーでした。
csharp
1 byte[] readPngFile(string path) { 2 using (FileStream fileStream = new FileStream (@""+path, FileMode.Open, FileAccess.Read)) { 3 BinaryReader bin = new BinaryReader (fileStream); 4 byte[] values = bin.ReadBytes ((int)bin.BaseStream.Length); 5 bin.Close (); 6 return values; 7 } 8 } 9
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/26 01:53 編集
2018/06/26 01:55
2018/06/26 02:03
2018/06/26 02:06 編集