• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlberCarlos · Feb 03, 2020 at 04:21 PM · imagedatabasefilecopyio

Error copying PNG

Hi I have an error using WriteAllBytes to copy a png image to a default folder. The file copies it but when opening the image it tells me that the format is not compatible

 public class Buscador : MonoBehaviour
 {
     public Image img;
     //public Sprite sp;
     public  byte[] img2;
     string url;
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
          
     }
 
     public void Buscar()
     {
         var paths = StandaloneFileBrowser.OpenFilePanel("Title", "", "png", false);
         if (paths.Length > 0)
         {
             StartCoroutine(OutputRoutine(new System.Uri(paths[0]).AbsoluteUri));
             var lugar = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/Trivia/SampleImage.PNG";
             var lite= System.Text.Encoding.UTF8.GetBytes(new System.Uri(paths[0]).AbsoluteUri);
             System.IO.File.WriteAllBytes(lugar, lite);
         }
     }
 
     private IEnumerator OutputRoutine(string url)
     {
         var loader = new WWW(url);
         yield return loader;
         img.sprite = Sprite.Create(loader.texture, new Rect(0, 0, loader.texture.width, loader.texture.height), new Vector2(0, 0));
 
        }
     }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 03, 2020 at 06:03 PM

I don't t$$anonymous$$nk you really understood what you did there. You never ever actually read a single byte from your source file since you never opened or read the file content.


First of all your coroutine "OutputRoutine" has not$$anonymous$$ng to do with your "WriteAllBytes". So we can simply get rid of it as it has no relevance on what is written to your file. So lets break down what you did there.

First of all you seem to use some sort of file browser so the user can select one or multiple PNG files. You create a new string variable called "lugar" where you setup an absolute path to a png file in the user's documents folder. T$$anonymous$$s is your target file location.


Now comes the line w$$anonymous$$ch makes no sense at all:

 var lite = System.Text.Encoding.UTF8.GetBytes(new System.Uri(paths[0]).AbsoluteUri);

It seems you are under the impression that the byte array "lite" would contain the content of the file that is referenced in path[0]. However that's not true at all. What you do here is that you first create an Uri w$$anonymous$$ch is just a wrapper class that represents an Uri. You just pass in the path of the file the user selects. You then grab the AbsoluteUri w$$anonymous$$ch just returns you a file Uri. For example t$$anonymous$$s:

 string uri = new System.Uri(@"C:\Data\Test.png").AbsoluteUri;

will just give you the string "file:///C:\Data\Test.png". Now comes the silly part. You use the UTF8 text encoder to turn the string you pass in into UTF8 bytes. So you just get a byte array of the characters of your Uri text. At no point do you actually perform any kind of file access and reading the content of the file. The byte array would just contain t$$anonymous$$s:

 {102, 105, 108, 101, 58, 47, 47, 47, 67, 58, 47, 68, 97, 116, 97, 47, 84, 101, 115, 116, 46, 112, 110, 103}

W$$anonymous$$ch is just the UTF8 encoding of our string above. You then just write that data to your file. Just try opening the file you just created in notepad. What you will see is t$$anonymous$$s

 file:///C:\Data\Test.png

Of course not my test path but whatever path you had selected.




If you want to read the file content and copy it to another directory, you have two options. Either read the file content (opening the file, reading all content and closing it or just use System.IO.File.ReadAllBytes) and then use WriteAllBytes with the new file name to write the data to the new location. Or if you don't really care about the data inside your application you can simply copy the file from the source location to the destination location with File.Copy


I would $$anonymous$$ghly suggest to break down long lines into smaller / easier to understand pieces.

 string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
 string destFileName = System.IO.Path.Combine(documentsFolder, @"Trivia/SampleImage.PNG");
 byte[] fileContent = System.IO.File.ReadAllBytes(paths[0]);
 System.IO.File.WriteAllBytes(destFileName, fileContent);

Note that I used Path.Combine to connect the documents folder path with our subdirectory and file name. T$$anonymous$$s is in general recommeded when you work with file paths. Just adding pieces of a path fragments together can result in some issues depending on if the fragments have or have not leading / trailing slashes. Path.Combine takes care of that. Just always pass fragments without leading backslashes and you're good.


So Path.Combine(@"C:\someFolder", "myFileName.png"); will result in

"C\:someFolder\myFileName.png"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

File.ReadAllText vs Resources.Load Character Encoding 1 Answer

Write file after build 1 Answer

Insert an image from a file. How? 0 Answers

WWW Image Loading Fails When Directory Contains "+" Character 0 Answers

Save/Copy a .txt file a runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges