Android Question Wrong File/ Contents

RJB

Active Member
Licensed User
Longtime User
I have a problem which I've noticed since updating for runtime permissions so I presume it's related.
I'm using:
B4X:
Dim ADir As String = ""
......
ADir = rp.GetSafeDirDefaultExternal("")
......
rp.GetSafeDirDefaultExternal("Receiver0")
......
etc.
to create folders. Then download jpg files to them. When new files are downloaded the old ones are deleted and the file names re-used, e.g. file0.jpg will have an image, file0.jpg is deleted and a different image is downloaded as file0.jpg.
However on reloading file0.jpg the app continues to show the original image, even though that image (should) no longer exist. I don't think it's the app. Opening the file with 'Photos' first shows the incorrect image, then the correct one, then again the incorrect one on closing. I have a video of this but can't upload it.

Any ideas as to what might cause this? Or how to 'permanently' delete the file so that it's contents can't be wrongly displayed?
 

KMatle

Expert
Licensed User
Longtime User
This is a normal behaviour as the filesystem does not update at once. I had this while creating pdf's wondering why it shows the "old ine". I think there's a method to do a refresh as you like (can't find it right now)
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
This is a normal behaviour as the filesystem does not update at once. I had this while creating pdf's wondering why it shows the "old ine". I think there's a method to do a refresh as you like (can't find it right now)
I thought it might be something like that so tried putting a delay (sleep(1000)) but it made no difference. Then I noticed what 'Photos' was doing. I can exit the app, open the file in 'Photos' several minutes later (and get wrong contents - right contents - wrong contents) then go back into the app and still get the wrong contents displayed!! surely the file system should have updated by then? However I would try the refresh but can't find it!
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
It sounds strange. What is the output of Log($"$datetime{File.LastModified}"$) after you download the files?

Where is the code that saves the files?
Log($...... gives the time of the download, i.e the time you would expect for the new file, not the old one.
The file is downloaded into the 'rp.GetSafeDirDefaultExternal("Receiver0")' folder then renamed to the 'file0.jpg' format:
B4X:
RenameFile(FileDetails0,FileDetails1, rpReceiverDir(Receiving), _ 'AdzDir & "/" & Receiver(Receiving), _
        RouteListString(Receiving, FrameNo, FileNameField))

Sub RenameFile(DirSource As String, FileSource As String, DirTarget As String, FileTarget As String) As Boolean
   If FileSource = Null Or FileTarget = Null Or DirSource = Null Or DirTarget = Null Then Return False
   If Not(File.Exists(DirSource, FileSource)) Or File.Exists(DirTarget, FileTarget) Then Return False
 Dim Old As String = File.Combine(DirSource, FileSource)
 Dim New As String = File.Combine(DirTarget, FileTarget)
 Dim ReturnValue As Boolean = False
 If New = Old Then Return False
   Dim R As Reflector, NewObj As Object
        NewObj = R.CreateObject2("java.io.File",Array As Object(New),Array As String("java.lang.String"))
  R.Target = R.CreateObject2("java.io.File",Array As Object(Old),Array As String("java.lang.String"))
  ReturnValue =  R.RunMethod4("renameTo", Array As Object(NewObj), Array As String("java.io.File"))
 File.Delete(DirSource, FileSource)
 If File.Exists(DirSource, FileSource) Then
  Log("exists")
 Else
  Log("does not exist")
 End If
 Return ReturnValue
End Sub
The log at this point says that the file with the downloaded filename no longer exists. There would then be two files in the folder.
When it's time to download the next set of files the folder is cleared with this code which shows something strange!:
B4X:
Sub ClearDirectory(DirectoryString As String)
 Try
  Dim FilesToDelete As List
  FilesToDelete.Initialize
  FilesToDelete.AddAll(File.ListFiles(DirectoryString))
  For i = 0 To FilesToDelete.Size -1
   Try
    File.Delete(DirectoryString, FilesToDelete.Get(i))
    Log("File.Combine(DirectoryString, FilesToDelete.Get(i)))
   Catch
    AddToELog("file not deleted: " & DirectoryString & "/" & FilesToDelete.Get(i) & ": " & LastException.Message) '23/7/13
   End Try
  Next
 Catch
  AddToELog("ClearDirectory: " & LastException.Message)
 End Try
End Sub
Now the log lists an additional file, three in total, which is named as the original downloaded filename, i.e. that was renamed above.
The files are apparently all deleted ready for the next download/ rename, as above.
I don't know if it has any bearing on the problem but the image is displayed in a WebView. Would that somehow store the old image?

does any of that help?

Thanks
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
The jobdone code is:
B4X:
public Sub ATPJobDone(job As ATPReturn)
  CallSub(ATPFunctions, "Disconnect")
  ServerLocation = RemoteServer
  Dim Coma As Int
  Coma = job.Name.IndexOf(",")
  FrameType = job.Name.SubString2(0, Coma)
  Dim Code As Int = job.Code.SubString2(0, 3)
  If Code = 200 Then
   If LastDownload.DLFile.ToLowerCase = "index.csv" Then
    LastIndexDate = job.Lastmodified
   End If
   DownloadDone(job.Success, job.name, job.Path, job.FileName, job.Lastmodified, job.Code)
  else If Code = 304 Then
   DownloadDone(job.Success, job.name, job.Path, job.FileName, job.Lastmodified, job.Code)
  else If (Code > 799) And (Code < 900) Then
   DownloadDone(job.Success, job.name, job.Path, job.FileName, job.Lastmodified, job.Code)
  Else
   DownloadDone(job.Success, job.name, job.Path, job.FileName, job.Lastmodified, "403")
  End If
End Sub

within 'DownloadDone' is the call that renames the file as above

B4X:
      RenameFile(FileDetails0,FileDetails1, rpReceiverDir(Receiving), _ 
                    RouteListString(Receiving, FrameNo, FileNameField))
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Further information:

ATPJobdone is called from within ATPFunctions by:
B4X:
Sub AStream_NewStream (Dir As String, FileName As String)
'this event is raised when a file was received successfully
ReceiveTimer.Enabled = False
 Try
  FailtimerReset(Stop)
  ReceivingFile = False
  If ReceivingCorrectFile And (ReceiveFile <> "") Then
   ReceiveFile = ""
   ATPInfo.name = RequestName
   ATPInfo.Success = True
   ATPInfo.Code = "200"
   ATPInfo.Path = Dir
   ATPInfo.FileName = FileName
   ATPInfo.LastModified = LastWriteTime
   CallSub2(CallingModule, "ATPJobDone", ATPInfo) 'ok
  Else
   'file not expected or wrong file so discard
   If File.Exists(Dir, FileName) Then
    File.Delete(Dir, FileName)
   End If
   ATPInfo.Success = False
   ATPInfo.name = RequestName
   ATPInfo.Code = "444" 'will be ignored, JD gives 403
   ATPInfo.Path = ""
   ATPInfo.FileName = ""
   ATPInfo.LastModified = LastWriteTime
   CallSub2(CallingModule, "ATPJobDone", ATPInfo) 'not OK, get or getifnewer
  End If
 Catch
  AddToLog("ATP File Receive: " & LastException.Message)
 End Try 
End Sub
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
The first thing to try is to call File.Copy instead of renameTo.
Same result!
Can I email you a video (only 266KB) which shows the file being opened by 'photos' which, I think indicates that the problem is outside of the app/ b4a?
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
As mentioned above I am using a WebView to show the image - by creating a file:
B4X:
  File.WriteString(rpDIRoot, "index.html", _
    "<html>" & CRLF & _
    "<style type=""text/css"">" & CRLF & _
    "<!--"  & CRLF & _
    "body {" & CRLF & _
    "background-image: url(" & DIImagePath & ");" & CRLF & _
    "background-repeat: no-repeat;"  & CRLF & _
    "background-size: 100% 100%;" & CRLF & _
    "}" & CRLF & _
    "-->" & CRLF & _
    "</style>" & CRLF & _
    "</body>" & CRLF & _
    "</html>")
then loading the url to the webview.
If I load the image to an imageview immediately before loading the url then the imageview always shows the correct image. The webview however doesn't.
I've tried loading the image as the .background for the webview instead using:
B4X:
  Dim bd As BitmapDrawable
  bd.Initialize(LoadBitmapSample("", DIImagePath, 400,300))
  bd.Gravity = Gravity.FILL
 DirectInputViewer(DINumber).Background = bd
  DirectInputViewer(DINumber).Invalidate
but this doesn't seem to work.
Maybe I'll have to give up on the image display!!
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
It is very confusing!
Two new threads:
WebView not updating correctly?
and
Unable to set WebView background
 
Upvote 0
Top