Android Question FileNotFound in DirInternal


Licensed User
Hi everyone,
Could somebody please help me out with this problem :

I am trying to load a .jpg image into an Imageview with the standard code : ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirInternal, Image, ImageView1.Width, ImageView1.Height, True))
In testing It works fine if the .jpg file is in DirAssets , but since I need to download the file first , it can't be placed in DirAssets so I download to DirInternal.
But I can't seem to get it to work, I keep getting file not found error. I put in all the read/write permissions in the Main module, just to be on the safe side, but still it won't work.
I check if the file exists and I get true, but still nothing.

Do I need to open an InputStream to fetch a file from DirInternal ?

Here is the code :

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    CallSub (Starter, "DataBaseSize")
    'Activity.AddMenuItem3("", "refresh", xui.LoadBitmapResize(File.DirAssets, "ic_cached_white_24dp.png", 32dip, 32dip, True), True)
    'Activity.AddMenuItem3("", "done", xui.LoadBitmapResize(File.DirAssets, "ic_done_white_24dp.png", 32dip, 32dip, True), True)
    'Dim bitmaps As List = Array("eni-logo-vector.png", "F1 ENEL.png", _
    '        "F3 SKY.png", "nuovo-logo-tim-.jpg","tkair.jpg","DD.jpg")
    Private dbsize As Int
    dbsize = Starter.IDHolder.Size
    PanelHolder= -1
    Dim LocExp As String
    Dim LocComp As String
    Dim LocAmount As String
    Dim LocCur As String
    Dim LocLogo As String
    Dim LocButton As Int
    For i = 0 To dbsize-1
    LocExp =  Starter.Expiry2holder.Get(i)
    LocComp = Starter.Companydueholder.Get(i)
    LocAmount =Starter.AmountHolder.Get(i)
    LocCur = "€"
    LocLogo =  Starter.Companydueholder.Get(i) & ".jpg"
    LocButton = i
        CLV1.Add(CreateItem(CLV1.AsView.Width, LocComp, LocLogo, LocCur, LocAmount, LocExp, LocButton), "")
    End Sub

Line 28 is where the the code searches for the file in DirInternal

[CODE=b4x]Private Sub CreateItem(Width As Int, Title As String,Image As String,Cur As String, Tot As String, ary As String, PaidButton As String) As Panel
'    GetData

    Dim p As B4XView = xui.CreatePanel("")

    Dim height As Int = 180dip

    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 150dip

    p.SetLayoutAnimated(0, 0, 0, Width, height)


    'p.SetColorandBorder(xui.Color_RGB(58,101,114),5dip,xui.Color_White ,7dip)

    'p.Width =20dip

    p.Tag= False

    lblExp.Text = ary

    lblContent.Text = Title

    Label1.Text = Cur

    Label3.Text = Tot

ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirInternal, Image, ImageView1.Width, ImageView1.Height, True))    [COLOR=rgb(184, 49, 47)]   [/COLOR]

Return p

End Sub

And here is the log :

icon downloaded ok
Icon file is found <----------------------here I log if the file exists in DirInternal.
northerngas.jpg <----------------------here I log the file name in DirInternal just to be sure.

** Activity (selectscreen) Pause, UserClosed = false **
** Activity (billlist) Create, isFirst = true **
DB size is:3
billlist_createitem (java line: 715) /data/user/0/simbills.beta/files/northerngas.jpg (No such file or directory) <------------------------ here the same file is not found
at Method)
at anywheresoftware.b4a.objects.streams.File.OpenInput(
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.initializeSampleImpl(
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeResize(
at anywheresoftware.b4a.keywords.Common.LoadBitmapResize(
at anywheresoftware.b4a.objects.B4XViewWrapper$XUI.LoadBitmapResize(
at simbills.beta.billlist._createitem(
at simbills.beta.billlist._activity_create(
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(
at simbills.beta.billlist.afterFirstLayout(
at simbills.beta.billlist.access$000(
at simbills.beta.billlist$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
--------- beginning of crash
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **


Licensed User
Hi Erel,
The code that downloads the file is not in the same activity as the code above, but I don't think that should be a problem, I didn't include this code because the file downloads checks out ok each time . It's in a service module , here is the code:

Sub AddToDataBase   (Company As String, InvNumber As String, InvDate As String , Expy As String, Expy2 As String, Year As String, Amount As Float,Reciept As String, Links As String )
    Private Query As String
    Dim IconLink As String = ""
    Query =  "INSERT INTO DueBills VALUES (NULL, ?, ?, ?, ?, ?, ?,?,?,?)"
    Starter.sql1.ExecNonQuery2(Query, Array As String (Company, InvNumber, InvDate, Expy,Expy2,Year, Amount, Reciept, ($" ${Company}${InvNumber}${Year}.pdf"$) ))
    DownloadAndSaveInv(Links, ($" ${Company}${InvNumber}${Year}.pdf"$))
    If File.Exists(File.DirInternal, ($" ${Company}.jpg"$))  = True Then
                DownloadAndSaveIcon (IconLink,($" ${Company}.jpg"$))
        End If
     End Try
End Sub

You can see from the highlighed logs in the sub below that the file downloads ok

Sub DownloadAndSaveIcon (Link As String, Filename As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log ( "icon downloaded ok ")
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, Filename, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
        If File.Exists(File.DirInternal, Filename) = True Then
            Log (" Icon file is found")
            Log (Filename)
                Log ( "Icon file is not found ")
                End If
        End If
Upvote 0


Licensed User
Great Erel, many thanks .
So with 'wait for' or 'sleep' it's basically like an async function in JavaScript. Actually I did think of that , but I always considered B4X as being purely synchronous, so my bad. But since I implemented the app in such a manner that until the service module is done downloading and saving the file, there is no way the other activity will try to load the ImageView , but anyway with a resumable sub the problem should go away . Will test it tomorrow . Thanks again .
Upvote 0