Android Question FileNotFound in DirInternal

Polaris

Member
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 :


B4X:
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.LoadLayout("1")
    '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), "")
    Next
    
    End Sub



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




B4X:
[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.LoadLayout("Card1")

    '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
[/CODE]




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)
java.io.FileNotFoundException: /data/user/0/simbills.beta/files/northerngas.jpg (No such file or directory) <------------------------ here the same file is not found
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.initializeSampleImpl(CanvasWrapper.java:601)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeResize(CanvasWrapper.java:548)
at anywheresoftware.b4a.keywords.Common.LoadBitmapResize(Common.java:1371)
at anywheresoftware.b4a.objects.B4XViewWrapper$XUI.LoadBitmapResize(B4XViewWrapper.java:674)
at simbills.beta.billlist._createitem(billlist.java:715)
at simbills.beta.billlist._activity_create(billlist.java:424)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at simbills.beta.billlist.afterFirstLayout(billlist.java:104)
at simbills.beta.billlist.access$000(billlist.java:17)
at simbills.beta.billlist$WaitForLayout.run(billlist.java:82)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
--------- beginning of crash
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
 

Polaris

Member
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:

B4X:
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 = "https://drive.google.com/File/d/1SkzrCe6H1_eB6WwLgK1v-c9US0ahtJOw/view?usp=sharing"
    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"$))
    
        Try
    If File.Exists(File.DirInternal, ($" ${Company}.jpg"$))  = True Then
                Return
            Else
                DownloadAndSaveIcon (IconLink,($" ${Company}.jpg"$))
    
        End If
    Catch
        Return
     End Try
End Sub


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

B4X:
Sub DownloadAndSaveIcon (Link As String, Filename As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    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)
            Else
                Log ( "Icon file is not found ")
                
                End If
        End If
 
Upvote 0

Polaris

Member
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
Top