- Home /
[Solved]copy data from resources to persistent data path
I want to copy files(images) from Resources to Application.persistentDataPath. Here's the code:
private void SaveFile (string fileName)
{
Texture2D texture = Resources.Load (System.IO.Path.GetFileNameWithoutExtension (fileName)) as Texture2D;
byte[] bytes = ?//i need to get byte array from texture
System.IO.File.WriteAllBytes (Application.persistentDataPath + folder, bytes);
}
The question is: can we get byte array of files in Resources folder? Or can we get byte information from textures?
UPD: if you want to copy file to persistent data path, you may use TextAssets(notice, your file has to have "bytes" extension on yr file to copy binary data).
Hi, How can i get byte array from the texture ? can you suggest ?
@Vamshi$$anonymous$$rishnaP, use texture.EncodeToPNG
Answer by VamshiKrishnaP · Feb 03, 2015 at 07:23 AM
Hi,
Here i followed like this and working perfect.
First i placed a ImageToUpload.png file in resources folder and renamed its extension .png to .bytes
Then called the below lines in void Start() to copy the image in persistentDataPath.
void Start(){
TextAsset imageTextAsset;
imageTextAsset = Resources.Load (System.IO.Path.GetFileNameWithoutExtension ("ImageToUpload")) as TextAsset;
File.WriteAllBytes(Application.persistentDataPath + "/ImageToUpload.png", imageTextAsset.bytes);
}
Well, a lot of things were changed since this time. Now it's much much easier:
Firstly, put your imageToUpload.png to resources (do not rename to .bytes extension)
And then write this code:
void Start () {
var texture = Resources.Load<Texture2D>("imageToUpload");//omit extension
File.WriteAllBytes(Application.persistentDataPath + "/imageToUpload.png", texture.encodeToPng());
That's it! You have copy of your image in persistent data path and you can change it.
Your answer
Follow this Question
Related Questions
C# Error when trying to load Texture at runtime 1 Answer
Load 2D Textures with Resource.Load doesn't work 4 Answers
Loading Custom Texture, Audio, Sprite at runtime (Like Resource Packs in minecraft ye) 0 Answers
Import Texture with good quality... 2 Answers
Does loading a Resource load that resource's dependencies? 1 Answer