Confusion for placing and accessing Data Folder

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Confusion for placing and accessing Data Folder

Post by pkmittal » Mon Jul 02, 2012 2:21 pm

Hi, I am having some problem on Andriod while accessing the data files.

I have data on "TEST Data" folder. To add this data to Andriod App, I went to standalone applicaton settings --copy files -- add folder and added the TEST Data folder.

TEST Data folder have couple of files. For example 1.jpg

I wrote the following code.

Code: Select all

on mouseUp
   
   set the folder to specialFolderPath("engine")
   
   put empty into tMyImages
   put empty into tFolders
   put empty into myFolder
   
   put the folders into tFolders
   answer  tFolders with "OK"
   
   if there is not a folder "TEST Data" then
      answer "TEST Data folder missing" with "OK"
      end if
         
   if there is not a file "TEST Data/1.jpg" then
      answer "1.jpg is missing" with "OK"
   end if
      
   
end mouseUp


When I run the above code on Andriod Device and also on emulator then tFolder display the list of folders.. and It shows Test Data folder... But the code inside the other if statements are also executed.. meaning it does not find TEST Data folder and also the file 1.jpg inside that folder...

I know that file system on Andriod are case sensitive.. but the folder name is created as used in the code..

Is there different way? Do I need to set something? The way I have added the Test Data folder is incorrect? or there is something wrong in the code....Please help me...

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Confusion for placing and accessing Data Folder

Post by Dixie » Mon Jul 02, 2012 2:48 pm

pkmittal..

You were nearly there... use this

Code: Select all

set the defaultfolder to specialFolderPath("engine")
be well

Dixie

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Confusion for placing and accessing Data Folder

Post by sturgis » Mon Jul 02, 2012 3:12 pm

Folder and defaultfolder are synonyms so that should be fine, I think the problem lies with the space in the folder name.

If you change it to this:

Code: Select all

on mouseUp
   
   set the folder to specialFolderPath("engine")
   
   put empty into tMyImages
   put empty into tFolders
   put empty into myFolder
   
   put the folders into tFolders
   answer  tFolders with "OK"
   
   if there is not a folder (quote & "TEST Data" & quote) then --puts quotes around the folder name
      answer "TEST Data folder missing" with "OK"
      end if
         
   if there is not a file (quote & "TEST Data/1.jpg" & quote) then
      answer "1.jpg is missing" with "OK"
   end if
      
   
end mouseUp

I don't know if it will work on android, but "shortfilepath" (check the dictionary) is really handy.

EDIT: Nope doesn't work on android.

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Confusion for placing and accessing Data Folder

Post by pkmittal » Mon Jul 02, 2012 4:07 pm

Thanks for the prompt responses..on windows it works with out extra quote also.. and folder and defaulfolder is also the same thing.....

I am asking for andriod.. Any idea why it is not workiing on andriod... what one has to change in the above code... is not it something trivial? Is there any any one who has done some programming on android can help?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Confusion for placing and accessing Data Folder

Post by sturgis » Mon Jul 02, 2012 5:25 pm

Hmm. Wondering of there is something going on with permissions.

Instead of "if there is a" you can put the folders into a variable as you do and then check to see if the folder name you're looking for is "among the lines of..."

But it seems as if there is more going on. I can't create a folder in specialfolderpath("documents") (error: Can't create that directory) but I SHOULd be able to. So I'm stumped.

Gene
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 75
Joined: Wed Mar 09, 2011 6:48 pm

Re: Confusion for placing and accessing Data Folder

Post by Gene » Mon Jul 02, 2012 6:34 pm

sturgis wrote:Hmm. Wondering of there is something going on with permissions.

Instead of "if there is a" you can put the folders into a variable as you do and then check to see if the folder name you're looking for is "among the lines of..."

But it seems as if there is more going on. I can't create a folder in specialfolderpath("documents") (error: Can't create that directory) but I SHOULd be able to. So I'm stumped.
It's entirely possible that I'm missing the point altogether, as I often do. However, here is a short block of code that should illustrate manipulating folders and files with the specialFolderPath etc. It's a button script that fires off a series of folder and file events:

Code: Select all

on mouseUp
   set the defaultFolder to specialfolderpath("Documents")
   answer the defaultFolder--shows the path to a folder named "files" rather than "documents," but what the heck

   create folder the defaultfolder&slash&"testfolder"--creates a new subfolder of "files"
   create folder the defaultfolder&slash&"another_folder"--creates a second folder just to have a list of folders to display
   answer the folders--displays subfolders of specialFolderPath("Documents") - aka "files"

   set the defaultFolder to specialfolderpath("Documents")&slash&"testfolder"--resets the defaultfolder in preparation to write a file
   answer the defaultfolder--this should be "testfolder"

   put "Hello from Testfolder" into temp
   put  temp into URL "file:testfile.txt" 
   answer the files--lists the files within the new default folder, "testfolder"
   answer URL "file:testfile.txt"--Contents of the text file can be read.  Q.E.D.
end mouseUp
I only tested this on Windows and on my Android phone, not the simulator. The simulator is a tool of the devil :shock: (just kidding, I think).

I hope this helps - Gene

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Confusion for placing and accessing Data Folder

Post by pkmittal » Tue Jul 03, 2012 5:03 am

Any Expert can tell me what is wrong with the code below? The engine folder shows the TEST Data folder.. but when we do if there is not a folder "TEST Data" is executed... meaning it does not find the TEST Data folder. and when we do the following

set the folder to specialFolderPath("engine") & slash & "TEST Data"

then it is able to find the files... please help.. let me know if my question is not clear enough.. please read the initial query in the quote below.. thanks
pradeep


pkmittal wrote:Hi, I am having some problem on Andriod while accessing the data files.

I have data on "TEST Data" folder. To add this data to Andriod App, I went to standalone applicaton settings --copy files -- add folder and added the TEST Data folder.

TEST Data folder have couple of files. For example 1.jpg

I wrote the following code.

Code: Select all

on mouseUp
   
   set the folder to specialFolderPath("engine")
   
   put empty into tMyImages
   put empty into tFolders
   put empty into myFolder
   
   put the folders into tFolders
   answer  tFolders with "OK"
   
   if there is not a folder "TEST Data" then
      answer "TEST Data folder missing" with "OK"
      end if
         
   if there is not a file "TEST Data/1.jpg" then
      answer "1.jpg is missing" with "OK"
   end if
      
   
end mouseUp


When I run the above code on Andriod Device and also on emulator then tFolder display the list of folders.. and It shows Test Data folder... But the code inside the other if statements are also executed.. meaning it does not find TEST Data folder and also the file 1.jpg inside that folder...

I know that file system on Andriod are case sensitive.. but the folder name is created as used in the code..

Is there different way? Do I need to set something? The way I have added the Test Data folder is incorrect? or there is something wrong in the code....Please help me...

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Confusion for placing and accessing Data Folder

Post by Klaus » Tue Jul 03, 2012 4:41 pm

Hi Pradeep,
pkmittal wrote:set the folder to specialFolderPath("engine") & slash & "TEST Data"
there is nothing wrong with this line! 8)
You should use more parenthesis however:
set the folder to (specialFolderPath("engine") & "/TEST Data")

Sometimes the engine will only evalutae the first part of the string (before the first &), therefore parens
should be used whenever possible and it makes sense!

But as I tried to point out in anohter thread, using relative filenames after setting the defaultfolder
does obviously NOT work as exspected on mobile devices, so try to always use absolute filenames!
...
put (specialfolderpath("engine") & "/TEST Data") into tFolderInQuestion
answer (there is a folder tFolderInQuestion)

put (specialfolderpath("engine") & "/TEST Data/1.jpg") into tFileInQuestion
answer (there is a file tFileInQuestion)
## should display TRUE if you added the files and folder via the "Copy files" tab in the standalonebuilder.
...

So your desired "copy files/folders from engine to docs folder" routine should look like this:

Code: Select all

command do_the_copy
  
  ## 1. create abolute pathnames!
  put (specialfolderpath("engine") & "/TEST Data") into tEngineFolderTEST
  put (specialfolderpath("documents") & "/TEST Data") into tDocsFolderTEST
  
  ## 2. Check for folder and create folder if necessary
  if there is not a folder tDocsFolderTEST then
    create folder tDocsFolderTEST
  end if
  
  ## 3. Now get the list of files in that folder:
  set the folder to tEngineFolderTEST
  put the files into tFiles2Copy
  
  ## 4. Now copy all the files to the docs folder in a repeat loop:
  repeat with i = 1 to the num of lines of tFiles2Copy
    
    ## 5. Again: Create absolute filenames, thsi will ALWAYS work! :-)
    put (tEngineFolderTEST & "/" & line i of tFiles2Copy) into tSourceFile
    put (tDocsFolderTEST & "/" & line i of tFiles2Copy) into tTargetFile
    
    ## 6. Check if files are already present, optional
    if there is NOT a file tTargetFile then
      put url("binfile:" & tSourceFile) into url("binfle:" & tTargetFile)
    end if
    
  end repeat
end do_the_copy
Do so for every folder/file you need to copy to the docs folder to be able to modify/edit them.

Best

Klaus

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Confusion for placing and accessing Data Folder

Post by pkmittal » Tue Jul 03, 2012 4:55 pm

Thanks.. I will try this code.. but it wil keep the files at 2 places.. 1) engline and 2) documents?

Is there not a way to directly have the files in documents folder when we install the app.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Confusion for placing and accessing Data Folder

Post by Klaus » Tue Jul 03, 2012 5:30 pm

pkmittal wrote:Thanks.. I will try this code.. but it wil keep the files at 2 places.. 1) engline and 2) documents?
Yes.
pkmittal wrote: Is there not a way to directly have the files in documents folder when we install the app.
You decide, you are the developer :D

No, since the app is "only" copied to the device, there is no install routine of any kind at all!
Therefore you need to take care of this when the app starts (the first time) e.g. with my script.

Please get used to it! 8)


Best

Klaus

Post Reply