Android Question Unable to delete file when exiting app

DT1111

Member
Licensed User
Longtime User
Hi

I thought the file will be deleted when the app exits ie when the UserClosed flag is true. But that is not the case.

What is wrong?

B4X:
Sub Activity_Create(FirstTime As Boolean)

        If File.Exists(File.DirRootExternal,"1.txt") = False Then
            File.OpenOutput(File.DirRootExternal,"1.txt",False)
        End If
end sub


B4X:
Sub Activity_Pause (UserClosed As Boolean)
        If UserClosed then
              File.Delete(File.DirRootExternal,"1.txt")
        end if
end sub
 

NJDude

Expert
Licensed User
Longtime User
There are 2 kinds of SD cards, Internal and External, the original poster is dealing with the internal (File.DirRootExternal).

The code works, however, you are creating that file on Activity_Create if doesn't exists, so this is what I see, you exit the activity and gets deleted, re-open the app (or return to the activity) and it's created again.
 
Upvote 0

DT1111

Member
Licensed User
Longtime User
Thank you all for the link and inputs.

The code works, however, you are creating that file on Activity_Create if doesn't exists, so this is what I see, you exit the activity and gets deleted, re-open the app (or return to the activity) and it's created again.

Granted the logic of sequence will recreate the "1.txt" but what I did was before relaunching the app, I checked if the "1.txt" was deleted after exiting the app, with FileManager. No it wasn't deleted. The point is the presence of the file was not the result of re-creation when the app is relaunched but rather the app was not deleting it upon exit.

I have changed my approach ie without deleting it but instead writing to it but that doesn't seem to work either

B4X:
Sub Activity_Create(FirstTime As Boolean)

        If File.Exists(File.DirRootExternal,"1.txt") = False Then
            File.OpenOutput(File.DirRootExternal,"1.txt",False)
            File.WriteString(File.DirRootExternal,"1.txt","Hello World")
        End If
end sub

B4X:
Sub Activity_Pause (UserClosed As Boolean)
        If UserClosed then
                  File.OpenOutput(File.DirRootExternal,"1.txt",False)
                  File.WriteString(File.DirRootExternal,"1.txt","Goodbye World")
        end if
end sub
 
Upvote 0

DT1111

Member
Licensed User
Longtime User
Hi Erel

I was reading the blogs as suggested by lemonisdead : http://www.b4x.com/android/fo...on-external-sd-with-kitkat.37592/#post-223941

The following were mentioned and I am trying to understand if writing to File.DirRootExternal (ie not SD card) will continue to work properly or was it superseded by KitKat.

And if I understand it correctly, writing to a folder based on package name is only applicable if one is writing to an SD card. In my case I am not.

There is nothing to solve or that can be solved. It is explained here: http://source.android.com/devices/tech/storage/index.html

If you want to write to a removable external card then you can only write to a folder based on your package name.

File.DirRootExternal / DirDefaultExternal continue to work properly.


This is how Google designed Android 4.4: https://plus.google.com/ TodLiebeck/posts/gjnmuaDM8sn

Starting from Android 4.4 you can only write to a folder based on your package name (on SD cards).

Apologies if I had quoted you out of context :)
 
Upvote 0

DT1111

Member
Licensed User
Longtime User
Sweet.

I know where the problem is.

I thought the UserClosed flag will fire when I exit the app by first selecting the "cascading" icon in the Android navigation button (the right most) and ending the app by swiping it away. No that didn't work.

But when I close the app correctly by selecting the end application navigation button (the left most), then the code works.

upload_2014-6-24_22-38-10.png


Thanks Erel.
 
Last edited:
Upvote 0
Top