Android Question File.Exists always returning true

MrKim

Well-Known Member
Licensed User
Longtime User
The following code always returns true. Over and over. The delete runs with no error. I know the file is not there because I deleted it.

B4X:
If File.Exists(Pth, FN) Then
    File.Delete(Pth, FN)
End If

Pth = /storage/emulated/0/Android/data/com.ISS.MobileJobLogging/files/Documents

FN = E529674-01.bmp

full sub below

B4X:
Sub DisplayPartPic(Pth As String, FN As String)
    Dim B As Bitmap, I As ImageView2
    Try
        B = LoadBitmap(Pth, FN)
        I.Initialize(B, PicPanl, Colors.LightGray, "1", Activity)
        PicPanl.Visible = True
    Catch
        'Msgbox("Error loading " & FN & CRLF & LastException, "DisplayPartPic")  'remmed 7/19/14 - Uneeded?
        If File.Exists(Pth, FN) Then
            File.Delete(Pth, FN)
        End If
        NoPic.Text="There was an error loading " & FN & CRLF & "Click here to view docs."
        NoPic.Visible=True
        NoPic.Left = PicPanl.Left
        NoPic.Top = PicPanl.Top
    End Try

End Sub

What I am doing - I copy a file from the network (elsewhere), load and display the bitmap in this routine.
If it fails to load it drops into the Catch, I assume it is a bad file and delete it.
I know the file is not there because I deleted it and put in a bad password (testing) so it fails to copy from the network. The same File.Exists(Pth, FN) is running elsewhere and also returning true, that is why it is dropping in to this routine - it thinks the file is there.
When the file IS there LoadBitmap runs fine

What am I missing?

below is the error that drops me in to the Catch:

B4X:
Error occurred on line: 2456 (Main)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:538)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:516)
    at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1315)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
This is line 2456:
B = LoadBitmap(Pth, FN)
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
The files folder are readonly. You can not delete/change files in this folder including subfolders
My files are in the subfolder Documents, and I have been adding to and deleting from that folder just fine. Everything works fine when it actually has access to the network. Files get copied to and deleted from

/storage/emulated/0/Android/data/com.ISS.MobileJobLogging/files/Documents

constantly. Normally everything works fine because the file IS there, as it should be. The only issue I am having is File.Exists is reporting the file IS there when it ISN'T.

If I am using the wrong location, what should I be using?

This is the code I use to determine where to put /Documents, which I found years ago and has always worked:
B4X:
        If File.ExternalWritable And File.ExternalReadable Then
            SKUtils.GlblNC.LD = File.DirDefaultExternal
        Else
            SKUtils.GlblNC.LD = File.DirInternal
        End If
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
1. File.DirDelete returns True if the file was deleted.
2. Can you upload a small example that reproduces this issue?
Evidently not, I tried reproducing the issue in a small example but it works fine.
Weird.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
I should have written my solution sooner. It seems to have to do with upping my target version to 26.
In any case I gave up on
File.DirDefaultExternal
and am only using
File.DirInternal
which seem to work fine.
 
Upvote 0
Top