I wrote some code to get all the folders inside a folder inside the StreamingAssets and then to get all the files in that folder.
It works well on Windows but I can't get it to work on Android.
Here is the code :
foreach (string s in Directory.GetDirectories(model.path + "/Levels")) {
GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
el.transform.SetParent(grid1.transform);
string[] words = s.Split('/');
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
words = s.Split('\\');
#endif
el.GetComponentInChildren<Text>().text = words[words.Length - 1];
el.GetComponent<Button>().onClick.AddListener(() => {
MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
});
}
I had to add the #if UNITY_STANDALONE_WIN for windows because of the '\'.
Then for the files inside that folder I wrote this :
foreach (string s in Directory.GetFiles(model.path + "/Levels/" + model.mapSize + "/" + model.colorNumber)) {
string[] words = s.Split('/');
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
words = s.Split('\\');
#endif
words = words[words.Length - 1].Split('.');
if (words[1] == "json") {
GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
el.transform.SetParent(grid3.transform);
el.GetComponentInChildren<Text>().text = words[0];
el.GetComponent<Button>().onClick.AddListener(() => {
levelButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
});
}
}
I'm using UnityEngine.UI.Directory but I read that it can't be use on Android and I must use WWW instead. Something like this :
string xmlSetUpPath = "jar:file://" + Application.dataPath + "!/assets/xml/xmlSetUp.xml";
WWW book1WWW = new WWW(book1Path);
yield return book1WWW;
if(!File.Exists(Application.persistentDataPath + "/xml/book1.xml"))
{
File.WriteAllBytes(Application.persistentDataPath + "/xml/book1.xml", book1WWW.bytes);
}
However I couldn't find anything like Directory.getFiles() or Directory.getFolder(). So I thought I could use WWW to check if the file exists (as the file names are 1.json, 2.json, 3.json) and iterate until a file doesn't exist.
But I would also need to check if a folder exists for the 1st part of the script and I can't find how to check if a folder exists with WWW. (My folders names are also 1,2,3,4...)
So how to check if a folder exists with WWW ?
Or what else can I do ?
UPDATE :
I updated the code, now it looks like this :
for (int i = 0; i < 100; i++) {
if (Directory.Exists(Path.Combine(model.path, "Levels/" + i.ToString()))){
GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
el.transform.SetParent(grid1.transform);
el.GetComponentInChildren<Text>().text = i.ToString();
el.GetComponent<Button>().onClick.AddListener(() => {
MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
});
}
}
I use path.combine() and check every folder from 0 to 100. It works on Windows but it still doesn't work in Android.
The path is set like this :
#if UNITY_IPHONE
path = Application.dataPath + "/Raw";
#endif
#if UNITY_ANDROID
path = "jar:file://" + Application.dataPath + "!/assets";
#endif
#if UNITY_STANDALONE || UNITY_EDITOR
path = Application.dataPath + "/StreamingAssets";
#endif
QUESTION UPDATE
I made a script that generate the file paths. The paths are always the same : "FolderNumerotedFromYToX/FolderNumerotedFromYToX/FileNumerotedFrom1ToX.json". The generation works but the writing to JSON doesn't. It outputs "{}".
// Generate level list
List<KeyValuePair<int, List<KeyValuePair<int, List<int>>>>> l;
l = new List<KeyValuePair<int, List<KeyValuePair<int, List<int>>>>>();
for (int i = 1; i < 100; i++) {
if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i.ToString()))) {
l.Add(new KeyValuePair<int, List<KeyValuePair<int, List<int>>>>(i, new List<KeyValuePair<int, List<int>>>()));
for (int j = 1; j < 100; j++) {
if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j))) {
l[l.Count - 1].Value.Add(new KeyValuePair<int, List<int>>(j, new List<int>()));
int k = 1;
while (true) {
if (File.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j + "/" + k + ".json"))) {
l[l.Count - 1].Value[l[l.Count - 1].Value.Count - 1].Value.Add(k);
}
else
break;
k++;
}
}
}
}
}
string ljson = JsonUtility.ToJson(l);
var lsr = File.CreateText("Assets/StreamingAssets/levels.json");
lsr.WriteLine(ljson);
lsr.Close();
Can you help me finding a way to store this to a file ?
It may sound hard, but you really have to use the WWW class to access your files from StreamingAssets on Android.
And you have to keep a list of your filenames, to be able to access them, as there seems to be no way to use something like File.Exists or Directory.GetFiles.
Example:
WWW www = new WWW(source);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
// make sure the destination directory exists
File.WriteAllBytes(destination, www.bytes);
}
else
{
// error handling
}
Your source files (base) path should look like this:
jar:file://" + Application.dataPath + "!/assets/
From the docs:
Note that on Android, the files are contained within a compressed .jar
file (which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file then you will need to use additional software to see
inside the .jar archive and obtain the file.
Source: http://docs.unity3d.com/Manual/StreamingAssets.html
To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file
#if UNITY_EDITOR
using UnityEditor.Build;
using UnityEditor;
using System.IO;
public class BM_AndroidBuildPrepartion : IPreprocessBuild
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildTarget target, string path)
{
// Do the preprocessing here
string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
{
foreach (string filename in fileEntries) {
sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
}
}
}
}
#endif
then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/
#if UNITY_ANDROID
string path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
WWW wwwfile = new WWW(path);
while (!wwwfile.isDone) { }
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
File.WriteAllBytes(filepath, wwwfile.bytes);
StreamReader wr = new StreamReader(filepath);
string line;
while ((line = wr.ReadLine()) != null)
{
//your code
}
#endif
Related
I am new to Unity and Android development, but please do not mark this as a duplicate - I've looked all over the stacks for an answer to this, but similar topics and questions haven't yielded any working solutions, or are wanting on details, or outdated, or seem not to fit my specific need.
Ok, I have a quiz app built from following part 1 & 2 of this tutorial. That link contains all the source files anyone might need for reference, and everything works fine on ios and in the unity editor, fyi.
The trouble is with the loadGameData function from the DataController script in Android. Again, everything works fine in iOS and the unity editor, but when I make an Android sdk, the quiz is blank and the console says the data couldn't be loaded.
Here is how the function is currently written (full file in tutorial link):
private void LoadGameData ()
{
string filePath = Path.Combine (Application.streamingAssetsPath, gameDataFileName);
if (File.Exists (result))
{
string dataAsJson = File.ReadAllText (result);
GameData loadedData = JsonUtility.FromJson<GameData> (dataAsJson);
allRoundData = loadedData.allRoundData;
} // #if(File.Exists...
else
{
Debug.LogError ("Cannot load game data!");
} // #else
} // #LoadGameData
If you check the same tutorial on youtube, you'll see lots of people have noted the same problem with the Android build and have been left unanswered. Same goes with unity forums - that's one reason why I don't think this question is a duplicate and could be helpful to others in a similar situation.
I've found that Android has always been sorta tricky with this and that there used to different ways of accessing a file based on platform, but these days "Application.streamingAssetsPath" should find the streaming assets directory on any platform, even Android.
What I've also learned is that in android, even if the path is correct, the file is compressed and will only return a url. So the url needs to be converted using unity's WWW class. I get that, but as of yet, I haven't been able to re-write my loadGameData function to work properly and load the allRoundData array.
Here's an example of some things I've tried:
IEnumerator androidData()
{
string filePath = Path.Combine (Application.streamingAssetsPath, gameDataFileName);
if (filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;
result = www.text;
} // #if(filePath.Contains
} // #androidData
private void LoadGameData ()
{
androidData();
if (File.Exists (result))
{
string dataAsJson = File.ReadAllText (result);
GameData loadedData = JsonUtility.FromJson<GameData> (dataAsJson);
allRoundData = loadedData.allRoundData;
} // #if(File.Exists...
else
{
Debug.LogError ("Cannot load game data!");
} // #else
} // #LoadGameData
I know I'm close, and this is probably simple -- but I just can't seem to get to the finish line on this. Can some one help me figure out how to write the loadGameData function so it will load this allRoundData array on android?
An example code would be awesome and much appreciated, not just by me, but I'm sure many others would appreciate it also - Thank you!
UPDATE:
Based on the first answer, I've tested some code that works on the unity editor, but crashes in Android. In the Unity editor, I get the "file already exists" message. Here is the code I've tested:
Already had: private string gameDataFileName = "data.json";
I added the copyFile call above loadGameDate in Start() and wrote the copy file and loadGameData functions like so ..
int copyFileToPersistentDataPath(string gameDataFileName)
{
string persistentPath = Application.persistentDataPath + "/" + gameDataFileName;
try
{
//Copy only if gameDataFileName does not exist
if (!System.IO.File.Exists(persistentPath))
{
string path = System.IO.Path.Combine(Application.streamingAssetsPath, gameDataFileName);
WWW www = new WWW(path);
while (!www.isDone) { }
System.IO.File.WriteAllBytes(persistentPath, www.bytes);
Debug.Log(gameDataFileName + " Successfully Copied File to " + persistentPath);
return 1;
}
else
{
Debug.Log(gameDataFileName + " File already exist here. There is no need to copy it again");
return 0;
}
}
catch (Exception e)
{
Debug.Log(gameDataFileName + " Failed To Copy File. Reason: " + e.Message);
return -1;
}
}
private void LoadGameData ()
{
string tempPath = Path.Combine(Application.persistentDataPath, gameDataFileName);
string dataAsJson = File.ReadAllText(tempPath);
GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);
allRoundData = loadedData.allRoundData;
} // #LoadGameData
This works with or without the call to copy the file in the editor, but crashes either way in Android.
I ended up putting the files in a Resources folder and going the resources.load a json file into a text asset and throw that into a string to parse route. I now have two different load functions, one that works in ios etc. and one that works in android. Here is the android function (the resourcesGameDataFile does not have the .json extension):
public void LoadDataForAndroid()
{
TextAsset dataFile = Resources.Load(resourcesGameDataFile) as TextAsset;
string dataAsJson = dataFile.ToString();
GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);
allRoundData = loadedData.allRoundData;
Debug.Log ("Android data loaded with" + resourcesGameDataFile);
} // #LoadDataForAndroid
And this works in the unity editor and in Bluestacks (android simulator).
As for loading and saving games, this is a duplicate. I marked and remove this as a duplicate because the answer in the duplicated questions did not explain how to read from the StreamingAssets folder. It only talked about saving and loading data.
Make sure that Write Permission is set to External (SDCard).
The first thing to do is to copy the file from StreamingAssets to the persistentDataPath location.
I've found reading data from Application.streamingAssetsPath problematic but I use two methods to solve this.
int copyFileToPersistentDataPath(string fileNameWithExtensionName)
{
string persistentPath = Application.persistentDataPath + "/" + fileNameWithExtensionName;
try
{
//Copy only if fileNameWithExtensionName does not exist
if (!System.IO.File.Exists(persistentPath))
{
string path = System.IO.Path.Combine(Application.streamingAssetsPath, fileNameWithExtensionName);
WWW www = new WWW(path);
while (!www.isDone) { }
System.IO.File.WriteAllBytes(persistentPath, www.bytes);
Debug.Log(fileNameWithExtensionName + " Successfully Copied File to " + persistentPath);
return 1;
}
else
{
Debug.Log(fileNameWithExtensionName + " File already exist here. There is no need to copy it again");
return 0;
}
}
catch (Exception e)
{
Debug.Log(fileNameWithExtensionName + " Failed To Copy File. Reason: " + e.Message);
return -1;
}
}
If that does not work for you, use the method with WebClient below:
void copyFileToPersistentDataPath(string fileNameWithExtensionName)
{
string path = System.IO.Path.Combine(Application.streamingAssetsPath, fileNameWithExtensionName);
string persistentPath = Application.persistentDataPath + "/" + fileNameWithExtensionName;
Debug.Log("Dir: " + persistentPath);
WebClient webClient = new WebClient();
webClient.Proxy = null;
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadComplete);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnUpdateDownloadProgress);
Uri uri = new Uri(path);
webClient.DownloadFileAsync(uri, persistentPath);
}
void OnDownloadComplete(object sender, AsyncCompletedEventArgs e)
{
Debug.Log("Finished Downloading: " + e.Error.Message);
}
void OnUpdateDownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
Debug.Log("Uploading Progreess: " + e.ProgressPercentage);
}
File Copy Usage:
copyFileToPersistentDataPath("yourFileName.txt");
After copying the file, you can then read and convert it to Json like this:
string fileNameWithExtensionName = "questionfile.txt";
string tempPath = Path.Combine(Application.persistentDataPath, fileNameWithExtensionName);
string dataAsJson = File.ReadAllText(fileNameWithExtensionName);
GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);
I'm going to use a pre-populated SQLite database in an Android game which I'm creating using Unity .
Since the simple way doesn't work on Android (It works perfect on Windows thou), I've followed this tutorial to use my database in an Android app.
public void OpenDB(string p) //p is the database name
{
// check if file exists in Application.persistentDataPath
string filepath = Application.persistentDataPath + "/" + p;
if(!File.Exists(filepath))
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + p); // this is the path to your StreamingAssets in android
while(!loadDB.isDone) {} // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDB.bytes);
}
//open db connection
connection = "URI=file:" + filepath;
dbcon = new SqliteConnection(connection);
dbcon.Open();
}
When I run this code, I get following error:
SqliteSyntaxException: file is encrypted or is not a database
Here's the full error:
SqliteSyntaxException: file is encrypted or is not a database
Mono.Data.SqliteClient.SqliteCommand.GetNextStatement (IntPtr pzStart,
System.IntPtr& pzTail, System.IntPtr& pStmt)
Mono.Data.SqliteClient.SqliteCommand.ExecuteReader (CommandBehavior
behavior, Boolean want_results, System.Int32& rows_affected)
Mono.Data.SqliteClient.SqliteCommand.ExecuteReader (CommandBehavior
behavior) Mono.Data.SqliteClient.SqliteCommand.ExecuteDbDataReader
(CommandBehavior behavior) System.Data.Common.DbCommand.ExecuteReader
() System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader
() dbAccess.SingleSelectWhere (System.String tableName, System.String
itemToSelect, System.String wCol, System.String wPar, System.String
wValue) (at Assets/dbAccess.cs:152)
I've downloaded the example from that post, and got the same error.
I'm suspicious about this line:
File.WriteAllBytes(filepath, loadDB.bytes);
and my thought is for some reason, it can't write the database data into the file.
Does anyone know how to solve this?
I solved the problem thanks to this blog.
This code works perfect on every platform due to the if statements which will do different behaviors depending on which platform the app is running on.
Here's the DataService.cs which do the important parts (or maybe I better say the whole part)
using SQLite4Unity3d;
using UnityEngine;
#if !UNITY_EDITOR
using System.Collections;
using System.IO;
#endif
using System.Collections.Generic;
public class DataService {
private SQLiteConnection _connection;
public DataService(string DatabaseName){
#if UNITY_EDITOR
var dbPath = string.Format(#"Assets/StreamingAssets/{0}", DatabaseName);
#else
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
if (!File.Exists(filepath))
{
Debug.Log("Database not in Persistent path");
// if it doesn't ->
// open StreamingAssets directory and load the db ->
#if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your StreamingAssets in android
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDb.bytes);
#elif UNITY_IOS
var loadDb = Application.dataPath + "/Raw/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WP8
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WINRT
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#else
var loadDb = Application.dataPath + "/StreamingAssets/" + DatabaseName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#endif
Debug.Log("Database written");
}
var dbPath = filepath;
#endif
_connection = new SQLiteConnection(dbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
Debug.Log("Final PATH: " + dbPath);
}
public void CreateDB(){
_connection.DropTable<Person> ();
_connection.CreateTable<Person> ();
_connection.InsertAll (new[]{
new Person{
Id = 1,
Name = "Tom",
Surname = "Perez",
Age = 56
},
new Person{
Id = 2,
Name = "Fred",
Surname = "Arthurson",
Age = 16
},
new Person{
Id = 3,
Name = "John",
Surname = "Doe",
Age = 25
},
new Person{
Id = 4,
Name = "Roberto",
Surname = "Huertas",
Age = 37
}
});
}
public IEnumerable<Person> GetPersons(){
return _connection.Table<Person>();
}
public IEnumerable<Person> GetPersonsNamedRoberto(){
return _connection.Table<Person>().Where(x => x.Name == "Roberto");
}
public Person GetJohnny(){
return _connection.Table<Person>().Where(x => x.Name == "Johnny").FirstOrDefault();
}
public Person CreatePerson(){
var p = new Person{
Name = "Johnny",
Surname = "Mnemonic",
Age = 21
};
_connection.Insert (p);
return p;
}
}
It follows by two other script to create or use the existing database.
ExistingDBScript.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class ExistingDBScript : MonoBehaviour {
public Text DebugText;
// Use this for initialization
void Start () {
var ds = new DataService ("existing.db");
//ds.CreateDB ();
var people = ds.GetPersons ();
ToConsole (people);
people = ds.GetPersonsNamedRoberto ();
ToConsole("Searching for Roberto ...");
ToConsole (people);
ds.CreatePerson ();
ToConsole("New person has been created");
var p = ds.GetJohnny ();
ToConsole(p.ToString());
}
private void ToConsole(IEnumerable<Person> people){
foreach (var person in people) {
ToConsole(person.ToString());
}
}
private void ToConsole(string msg){
DebugText.text += System.Environment.NewLine + msg;
Debug.Log (msg);
}
}
CreateDBScript.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class CreateDBScript : MonoBehaviour {
public Text DebugText;
// Use this for initialization
void Start () {
StartSync();
}
private void StartSync()
{
var ds = new DataService("tempDatabase.db");
ds.CreateDB();
var people = ds.GetPersons ();
ToConsole (people);
people = ds.GetPersonsNamedRoberto ();
ToConsole("Searching for Roberto ...");
ToConsole (people);
}
private void ToConsole(IEnumerable<Person> people){
foreach (var person in people) {
ToConsole(person.ToString());
}
}
private void ToConsole(string msg){
DebugText.text += System.Environment.NewLine + msg;
Debug.Log (msg);
}
}
And the person script, demonstrates the person table in the database
using SQLite4Unity3d;
public class Person {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public override string ToString ()
{
return string.Format ("[Person: Id={0}, Name={1}, Surname={2}, Age={3}]", Id, Name, Surname, Age);
}
}
Also you need to add plugins and Sqlite.cs to your project which you can find in the git repository
It helped me to overcome the issue, Hope it help the others as well.
I'm trying to play some mp3 files (which app downloaded from the remote server) from sd card using Cordova.
I tried to use following:
if($ionicPlatform.is('android')){
src = 'yourPersistantAppFolder/main_expansion/' + src;
}
and
if($ionicPlatform.is('android')){
src = 'sdcard/yourPersistantAppFolder/main_expansion/' + src;
}
But without luck.
I tried to find some anwser here:
https://github.com/apache/cordova-plugin-file
But without the luck.
Many thanks for any advice.
Edit:
Path in Android is displayed like this:
This may help:
(jc??? are mysupport routines)
import { File } from "#ionic-native/file";
import { Diagnostic } from "#ionic-native/diagnostic";
constructor(
...
public file: File,
public diagnostic: Diagnostic
){
this.diagnostic.getExternalSdCardDetails()
.then( (data) => {
this.jcError += "\n" + "sd:" + JSON.stringify( data);
this.jcError += "\n" + "Number cards: " + data.length;
for( let ii = 0; ii < data.length; ii += 1){
let thisElem = data[ii];
if( thisElem.type.toLowerCase() === "application" && thisElem.canWrite){
this.ourSDentry = thisElem;
basePathSD = thisElem.filePath;
break;
}
}
if( !basePathSD){
this.jcError += "\n\n" + "no SD card found";
return;
}
}, (errData)=>{
tag = "getExternalSdCardDetails";
this.jcError += "\n\n" + tag + ":ERR:" + JSON.stringify( errData);
});
Error in checking the file.
I have use one code. But i think there is some problem. The file got stuck to one file. And always keep dowloading the same file.
here is how i code this. Here in the below code. I am trying to download the images to local sdcard. If the file is already available i dont want it to download. And if not, then download. But it seems its just keep repeat download the same file.
function appReady(datadira){
$(".gallery").html("Ready to check remote files...");
$.get("http://www.pankhida.com/main/?json=get_page&page_id=115&callback=?", {}, function(res) {
if (res.page.attachments.length > 0) {
$(".gallery").html("Going to sync some images...");
for (var i = 0; i < res.page.attachments.length; i++) {
if (knownfiles.indexOf(res.page.attachments[i].url) == -1) {
//alert("need to download " + res[i]);
var ft = new FileTransfer();
var fullPath = res.page.attachments[i].url;
var filename = fullPath.replace(/^.*[\\\/]/, '');
var dlPath = DATADIR.toURL() + "/" + filename;
var uri= res.page.attachments[i].url;
uri = encodeURI(uri);
//console.log("downloading crap to " + dlPath);
window.resolveLocalFileSystemURL(DATADIR.toURL() +"/"+ filename, appStart, function(){
ft.download(uri, dlPath, function(e){
//renderPicture(e.toURL());
alert("Successful download of "+e.toURL());
}, onError, true);
});
}
}
}
$(".gallery").html("");
}, "json");
}
I am using window.resolveLocalFileSystemURL which i got from this URL http://www.raymondcamden.com/2014/7/1/Cordova-Sample-Check-for-a-file-and-download-if-it-isnt-there.
Please suggest where i am wrong. Or any correct code will be really helpful.
It has to do with the closure inside resolveLocalFileSystemURL.The value of ft ends up being the last one used from the loop. Try changing
for (var i = 0; i < res.page.attachments.length; i++) {
to
res.page.attachments.forEach(function(attachment) {
and the last } to }).
Inside the loop, use attachment instead of res.page.attachments[i].
Is it possible to download a folder and its contents using air for android? I would like to also use the same code on iPhone.
I need it to be stored in the application storage so we can reference the content once downloaded.
I know it's possible to download content as referenced in this question: Download Content using Air For Android
but can a person download the entire folder and again store it in the application directory?
Thanks
#lukevain? :)
Are you downloading this from a web server? If so this is not going to work and I would recommend using a php script to list all the files in the remote directory.
If this is local you can use this:
//using the application storage dir. This will work on iOS and Android
var f:File = File.applicationStorageDirectory.resolvePath("yourFolder/");
//you can also use getDirectoryListingAsync to have this run in the background
var filesList:Array = f.getDirectoryListing();
for (var i:int = 0; i < filesList.length; i++) {
trace(filesList[i].nativePath);
}
This is not the answer of how to get contents of a folder from the server but it is a way to search through a folder of your choice on the local machine. It only goes 3 levels deep and it may not be the best way to find folders because it checks to see if their extension is null and I made it assume it is a folder. Then I search in that folder. Anyway here it is.
You will need do create a text field and give it an instance name of "daText";
Here is the code:
import flash.filesystem.*;
import flash.media.*;
var myFolder:String = "images/"; /// change this to the folder of your choice :)
var desktop:File = File.applicationDirectory.resolvePath(myFolder);
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
if (files[i].extension == null)
{
daText.text += files[i].name + "\n";
var anotherArray:File = File.applicationDirectory.resolvePath(myFolder+files[i].name);
var h:Array = anotherArray.getDirectoryListing();
for (var a:uint = 0; a < h.length; a++)
{
daText.text += "----" + h[a].name + "\n";
if (h[a].extension == null)
{
var oneMoreArray:File = File.applicationDirectory.resolvePath(myFolder+files[i].name+"/"+h[a].name+"/");
var C:Array = oneMoreArray.getDirectoryListing();
for (var B:uint = 0; B < C.length; B++)
{
daText.text += "*******"+C[B].name + "\n";
}
}
}
}
}