Android Question i still get the old file even after delete please help

Makumbi

Well-Known Member
Licensed User
i use this code to delete a file but even after deleting using this code iam still gettting the oldfter please help . i there a way of refreshing
B4X:
    Dim FilePath2 As String = File.Combine(File.DirRootExternal, "Download/P5WORK.PDF")
    Dim FileName As String = "P5WORK.PDF"
    File.Delete(FilePath2, FileName)

here is the whole code

B4X:
Dim FilePath2 As String = File.Combine(File.DirRootExternal, "Download/P5WORK.PDF")
    Dim FileName As String = "P5WORK.PDF"
    File.Delete(FilePath2, FileName)
    Dim Phone As Phone
    If Phone.SdkVersion <= 18 Then
        Dim i As Intent
        i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & FilePath2)
        Dim p As Phone
        p.SendBroadcastIntent(i)
    Else
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim MediaScannerConnection As JavaObject
        MediaScannerConnection.InitializeStatic("android.media.MediaScannerConnection")
        Dim interface As Object = MediaScannerConnection.CreateEventFromUI("android.media.MediaScannerConnection.OnScanCompletedListener", "ScanCompleted", _
                   Null)
        MediaScannerConnection.RunMethod("scanFile", Array(ctxt, Array As String(FilePath2), Array As String("image/pdf"), interface))
    End If
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
      
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "P5WORK.PDF", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
              
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            File.Copy(File.DirInternal, "P5WORK.PDF", File.DirRootExternal, "Download/P5WORK.PDF")
            Dim FilePath As String = File.Combine(File.DirRootExternal, "Download/P5WORK.PDF")
            Dim Phone As Phone
            If Phone.SdkVersion <= 18 Then           ' min - 4.3.1
                Dim i As Intent
                i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & FilePath)
                Phone.SendBroadcastIntent(i)
            Else
                Dim ctxt As JavaObject
                ctxt.InitializeContext
                Dim MediaScannerConnection As JavaObject
                MediaScannerConnection.InitializeStatic("android.media.MediaScannerConnection")
                Dim interface As Object = MediaScannerConnection.CreateEventFromUI("android.media.MediaScannerConnection.OnScanCompletedListener", "ScanCompleted", _
                   Null)
                MediaScannerConnection.RunMethod("scanFile", Array(ctxt, Array As String(FilePath), Array As String("image/pdf"), interface))
            End If
        End If
      
    End If
 

Chris2

Active Member
Licensed User
I think you have the filename twice in the delete path, so you're not actually deleting it.

Try
B4X:
Dim FilePath2 As String = File.Combine(File.DirRootExternal, "Download")
    Dim FileName As String = "P5WORK.PDF"
    File.Delete(FilePath2, FileName)

or just
B4X:
File.Delete(File.DirRootExternal, "Download/P5WORK.PDF")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Also make sure to have the right permissions for this path. You need to use Runtimepermissions and request permission for this path. Also note to have the correct Manifest entries for the permission.

Furthermore on higher Android you can not use file uris (file://*) any longer. You need to use FileProvider to share/open the file in another app.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
File.Delete(File.DirRootExternal, "Download/P5WORK.PDF")
better is to check the RESULT.
B4X:
if File.Delete(File.DirRootExternal, "Download/P5WORK.PDF") then
  LOG("Successfully deleted")
else
  LOG("File NOT deleted")
end if
 
Upvote 0
Top