Deleting file from Android DocumentsDirectory

I am using a simple code in COrona to delete a file from the system.DocumentsDirectory. It works fine on the simulator but not on Android device.

    filePath = system.pathForFile( "test.txt", system.DocumentsDirectory )     os.remove( filePath  )

I can create files simply by the following on the same device.

    local filePath = system.pathForFile("test.txt", system.DocumentsDirectory )     file = io.open( filePath, "a" )

How do you know its not working?

Are you getting any error messages?

I have a tableview that lists all files in that folder. I get no error message. The table refreshes evetytime a file added or deleted. I can send the full code if you need.

Hi Rob, I tested my code on iPhone and Mac and it works fine on both simulator and iPhone. this has to be something about Andorid. 

I tried all sort of combination for android and it doesnt work. Following is my full code. I just copied main part that is related and added a comment “This doesnt work.”

-- Project: StructuralCompass -- File name: main.lua -- -- Author: Amir Farazmand -- Supports Graphics 2.0 --------------------------------------------------------------------------------------- local widget = require( "widget" ) local lfs = require( "lfs" ) &nbsp;function projectnamefield( event ) if ( "began" == event.phase ) then textMode = true elseif ( "ended" == event.phase ) then -- This event is called when the user stops editing a field: for example, when they touch a different field &nbsp; &nbsp; &nbsp; &nbsp; textField.isVisible = false &nbsp; &nbsp; &nbsp; &nbsp; tableView.isVisible = true &nbsp; &nbsp; &nbsp; &nbsp; local filePath = system.pathForFile( textField.text, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp; &nbsp; file = io.open( filePath, "a" ) io.close( file ) &nbsp; listFile() elseif ( "submitted" == event.phase ) then -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard tableView.isVisible = true -- Hide keyboard native.setKeyboardFocus( nil ) textMode = false textField.isVisible = false local filePath = system.pathForFile( textField.text, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp; &nbsp; file = io.open( filePath, "a" ) io.close( file ) &nbsp; listFile() end end ------------File list local function mainMenu( &nbsp;) &nbsp;function listFile( ) fileNo=0 fileName = {} \_W=320 \_H=480 &nbsp; local doc\_path = system.pathForFile( "", system.DocumentsDirectory ) &nbsp;for file in lfs.dir(doc\_path) do &nbsp; fileNo = fileNo+1 &nbsp; fileName[fileNo]=file &nbsp; end &nbsp; if fileNo \<3 then fileNo=3 end -- No file recorded yet &nbsp;function delProject( event ) &nbsp; if event.action == "clicked" then &nbsp; print ("yes") &nbsp; end &nbsp; local alert = native.showAlert( selectedFile, "Due to Android limitation deleting files individually is not possible. You can delete all your projects from the Android App Settings.", { "Ok" } ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filePath = system.pathForFile( selectedFile, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp;os.remove( filePath &nbsp;) -- This doesnt work. &nbsp; &nbsp; &nbsp;listFile() &nbsp;end &nbsp; local function onRowRender( event ) &nbsp; &nbsp; &nbsp;-- Get reference to the row group &nbsp; &nbsp; local row = event.row &nbsp; &nbsp; -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added &nbsp; &nbsp; local rowHeight = row.contentHeight &nbsp; &nbsp; local rowWidth = row.contentWidth &nbsp; &nbsp; &nbsp;if jobText then jobText =nil end &nbsp; &nbsp; &nbsp;local jobText = display.newText( row, row.id, 0, 0, systemFontBold, \_W\*.04 ) &nbsp; &nbsp; jobText:setFillColor( 0,0,0 ) &nbsp; &nbsp; jobText.anchorX = 0 &nbsp; &nbsp; jobText.x = \_W\*.08 &nbsp; &nbsp; jobText.y = rowHeight \* 0.55 &nbsp; -- Precalculate y position. NOTE: row's height will change as we add children &nbsp; --local rowArrow = display.newImage( row, "rowArrow.png", false ) &nbsp; -- Right-align the arrow &nbsp; --rowArrow.anchorX = 1 &nbsp; --rowArrow.x = rowWidth - \_W/6 &nbsp; --rowArrow.y = rowHeight\*.5 &nbsp;end local function onRowTouch( event) local function projectOption( event ) &nbsp; &nbsp;if event.action == "clicked" then &nbsp; &nbsp; &nbsp; &nbsp; local i = event.index &nbsp; &nbsp; &nbsp; &nbsp; if i == 1 then &nbsp; &nbsp; &nbsp; &nbsp; local alert = native.showAlert( "ALERT", "Are you sure to delete this file? &nbsp;"..selectedFile, { "Yes", "No" }, delProject ) &nbsp; &nbsp; &nbsp; &nbsp; elseif i == 3 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startRecording() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tableView.isVisible=false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; end &nbsp; end &nbsp; &nbsp;selectedFile = event.row.id &nbsp; &nbsp;if selectedFile == "Create a New Project or Open existing below" then&nbsp; &nbsp; &nbsp; tableView.isVisible=false local tHeight = 35 if isAndroid then tHeight = 35 end -- adjust for Android textField = native.newTextField( 155, 20, 280, tHeight ) textField.text = "EnterProjectName" textField:addEventListener( "userInput", projectnamefield ) &nbsp; &nbsp;else&nbsp; &nbsp; local alert = native.showAlert( "Project", "What to do with this file? &nbsp;"..selectedFile, { "Delete Project", "Cancel", "Open" }, projectOption ) end end -- Create the file List &nbsp; tableView = widget.newTableView { &nbsp; &nbsp; left = 0, &nbsp; &nbsp; top = 0, &nbsp; &nbsp; height = 330, &nbsp; &nbsp; width = 300, &nbsp; &nbsp; onRowRender = onRowRender, &nbsp; &nbsp; onRowTouch = onRowTouch, &nbsp; &nbsp; listener = scrollListener } -- Insert 40 rows &nbsp; for i = 1, fileNo-2 do &nbsp; &nbsp; -- Insert a row into the tableView &nbsp; &nbsp; if i==1 then tableView:insertRow{id = "Create a New Project or Open existing below"} end &nbsp; &nbsp; tableView:insertRow{id = fileName[i+2]} &nbsp; end end-- end of listFile listFile() end --end of main menu timer.performWithDelay( 500, mainMenu )

When dealing with File I/O, you should examine results codes when they exist. They may be a clue to what is actually wrong.

local destDir = system.DocumentsDirectory -- where the file is stored local results, reason = os.remove( system.pathForFile( "apple.txt", destDir ) ) if results then &nbsp; &nbsp; print( "file removed" ) else &nbsp; &nbsp; print( "file does not exist", reason )\ end

The file delete works fine. but it has to do something with Native alerts. Currently, there is a chain of 3 alerts to delete the files, would you think this might be a problem?

I actually built a loop for Native Alert on Android and it ends the loop after second repeat. Is this a security feature for Android?!

How do you know its not working?

Are you getting any error messages?

I have a tableview that lists all files in that folder. I get no error message. The table refreshes evetytime a file added or deleted. I can send the full code if you need.

Hi Rob, I tested my code on iPhone and Mac and it works fine on both simulator and iPhone. this has to be something about Andorid. 

I tried all sort of combination for android and it doesnt work. Following is my full code. I just copied main part that is related and added a comment “This doesnt work.”

-- Project: StructuralCompass -- File name: main.lua -- -- Author: Amir Farazmand -- Supports Graphics 2.0 --------------------------------------------------------------------------------------- local widget = require( "widget" ) local lfs = require( "lfs" ) &nbsp;function projectnamefield( event ) if ( "began" == event.phase ) then textMode = true elseif ( "ended" == event.phase ) then -- This event is called when the user stops editing a field: for example, when they touch a different field &nbsp; &nbsp; &nbsp; &nbsp; textField.isVisible = false &nbsp; &nbsp; &nbsp; &nbsp; tableView.isVisible = true &nbsp; &nbsp; &nbsp; &nbsp; local filePath = system.pathForFile( textField.text, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp; &nbsp; file = io.open( filePath, "a" ) io.close( file ) &nbsp; listFile() elseif ( "submitted" == event.phase ) then -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard tableView.isVisible = true -- Hide keyboard native.setKeyboardFocus( nil ) textMode = false textField.isVisible = false local filePath = system.pathForFile( textField.text, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp; &nbsp; file = io.open( filePath, "a" ) io.close( file ) &nbsp; listFile() end end ------------File list local function mainMenu( &nbsp;) &nbsp;function listFile( ) fileNo=0 fileName = {} \_W=320 \_H=480 &nbsp; local doc\_path = system.pathForFile( "", system.DocumentsDirectory ) &nbsp;for file in lfs.dir(doc\_path) do &nbsp; fileNo = fileNo+1 &nbsp; fileName[fileNo]=file &nbsp; end &nbsp; if fileNo \<3 then fileNo=3 end -- No file recorded yet &nbsp;function delProject( event ) &nbsp; if event.action == "clicked" then &nbsp; print ("yes") &nbsp; end &nbsp; local alert = native.showAlert( selectedFile, "Due to Android limitation deleting files individually is not possible. You can delete all your projects from the Android App Settings.", { "Ok" } ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filePath = system.pathForFile( selectedFile, system.DocumentsDirectory ) &nbsp; &nbsp; &nbsp;os.remove( filePath &nbsp;) -- This doesnt work. &nbsp; &nbsp; &nbsp;listFile() &nbsp;end &nbsp; local function onRowRender( event ) &nbsp; &nbsp; &nbsp;-- Get reference to the row group &nbsp; &nbsp; local row = event.row &nbsp; &nbsp; -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added &nbsp; &nbsp; local rowHeight = row.contentHeight &nbsp; &nbsp; local rowWidth = row.contentWidth &nbsp; &nbsp; &nbsp;if jobText then jobText =nil end &nbsp; &nbsp; &nbsp;local jobText = display.newText( row, row.id, 0, 0, systemFontBold, \_W\*.04 ) &nbsp; &nbsp; jobText:setFillColor( 0,0,0 ) &nbsp; &nbsp; jobText.anchorX = 0 &nbsp; &nbsp; jobText.x = \_W\*.08 &nbsp; &nbsp; jobText.y = rowHeight \* 0.55 &nbsp; -- Precalculate y position. NOTE: row's height will change as we add children &nbsp; --local rowArrow = display.newImage( row, "rowArrow.png", false ) &nbsp; -- Right-align the arrow &nbsp; --rowArrow.anchorX = 1 &nbsp; --rowArrow.x = rowWidth - \_W/6 &nbsp; --rowArrow.y = rowHeight\*.5 &nbsp;end local function onRowTouch( event) local function projectOption( event ) &nbsp; &nbsp;if event.action == "clicked" then &nbsp; &nbsp; &nbsp; &nbsp; local i = event.index &nbsp; &nbsp; &nbsp; &nbsp; if i == 1 then &nbsp; &nbsp; &nbsp; &nbsp; local alert = native.showAlert( "ALERT", "Are you sure to delete this file? &nbsp;"..selectedFile, { "Yes", "No" }, delProject ) &nbsp; &nbsp; &nbsp; &nbsp; elseif i == 3 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startRecording() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tableView.isVisible=false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; end &nbsp; end &nbsp; &nbsp;selectedFile = event.row.id &nbsp; &nbsp;if selectedFile == "Create a New Project or Open existing below" then&nbsp; &nbsp; &nbsp; tableView.isVisible=false local tHeight = 35 if isAndroid then tHeight = 35 end -- adjust for Android textField = native.newTextField( 155, 20, 280, tHeight ) textField.text = "EnterProjectName" textField:addEventListener( "userInput", projectnamefield ) &nbsp; &nbsp;else&nbsp; &nbsp; local alert = native.showAlert( "Project", "What to do with this file? &nbsp;"..selectedFile, { "Delete Project", "Cancel", "Open" }, projectOption ) end end -- Create the file List &nbsp; tableView = widget.newTableView { &nbsp; &nbsp; left = 0, &nbsp; &nbsp; top = 0, &nbsp; &nbsp; height = 330, &nbsp; &nbsp; width = 300, &nbsp; &nbsp; onRowRender = onRowRender, &nbsp; &nbsp; onRowTouch = onRowTouch, &nbsp; &nbsp; listener = scrollListener } -- Insert 40 rows &nbsp; for i = 1, fileNo-2 do &nbsp; &nbsp; -- Insert a row into the tableView &nbsp; &nbsp; if i==1 then tableView:insertRow{id = "Create a New Project or Open existing below"} end &nbsp; &nbsp; tableView:insertRow{id = fileName[i+2]} &nbsp; end end-- end of listFile listFile() end --end of main menu timer.performWithDelay( 500, mainMenu )

When dealing with File I/O, you should examine results codes when they exist. They may be a clue to what is actually wrong.

local destDir = system.DocumentsDirectory -- where the file is stored local results, reason = os.remove( system.pathForFile( "apple.txt", destDir ) ) if results then &nbsp; &nbsp; print( "file removed" ) else &nbsp; &nbsp; print( "file does not exist", reason )\ end

The file delete works fine. but it has to do something with Native alerts. Currently, there is a chain of 3 alerts to delete the files, would you think this might be a problem?

I actually built a loop for Native Alert on Android and it ends the loop after second repeat. Is this a security feature for Android?!