Android Question Problem showing an image in imageview after downloading from FTP

Mentalist

Member
Licensed User
Longtime User
Hello,

can someone please show me, how lo add an Image to an ImageView?
I am too stupid. :-(

example:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ImageView As ImageView
    Private Button1 As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Button1_Click
    If File.Exists(File.DirInternal, "test.png") = True Then
        ImageView.Bitmap = LoadBitmap(File.DirInternal, "test.png")
    End If
End Sub

I always get "java.lang.RuntimeException: Error loading bitmap.", although the image exists.

Thanks!
 
Last edited:
Solution
The second argument of FTP.DownloadFile is AsciiFile As Boolean.
You've set to true, but an image is not an ascii file. Change it to false and it will work.

1684935664177.png

drgottjr

Expert
Licensed User
Longtime User
or the image may not be an image or its format may not be supported (calling it a ".png" doesn't make it a .png) or it's corrupted.
if it's too large for available memory, i think op will have the same problem trying to re-size. few details were provided.
 
Upvote 0

zed

Active Member
Licensed User
ImageView.Bitmap = LoadBitmap(File.DirInternal, "test.png")
replace File.DirInternal with XUI.DefaultFolder
look here
 
Upvote 0

Mentalist

Member
Licensed User
Longtime User
No problems, when I copy the file into DirAssets and run the app.
But I still get this error and the app chrashes:

Error:
Error occurred on line: 217 (Main)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.initializeSampleImpl(CanvasWrapper.java:637)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeResize(CanvasWrapper.java:548)
    at anywheresoftware.b4a.keywords.Common.LoadBitmapResize(Common.java:1404)
    at anywheresoftware.b4a.objects.B4XViewWrapper$XUI.LoadBitmapResize(B4XViewWrapper.java:715)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:150)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8757)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **

When I copy the file from DirAssets to the XUI.DefaultFolder everything is fine, but I am downloading the file from a server.
And: I use CallSubDelayed to wait for the sub to finish downloading and then check if the file exists.
File is okay.

Thanks for SetBitmap and XUI.DefaultFolder (good info). :)
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I use CallSubDelayed to wait for the sub to finish downloading
Why not just use "Wait For" ?

 
Upvote 0

Mentalist

Member
Licensed User
Longtime User
I use CallSubDelayed like this:
test:
Private Sub DLpngSub
    Sleep(1)

    ctm.InitializeAcceptAll
    FTP.Initialize("xxx", "xxx", 21, "xxx", "xxx")
    FTP.PassiveMode = True
    FTP.UseSSLExplicit = True
    FTP.SetCustomSSLTrustManager(ctm)
    FTP.SendCommand("CWD", "/test/png")
    If FTP.IsInitialized=True Then
        Log("ok")
    else if FTP.IsInitialized=False Then
        Log("No")
    End If

    Dim sf1 As Object = FTP.DownloadFile("test.png", True, xui.DefaultFolder, "test.png")
    Wait For (sf1) ftp_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then
        'Sleep(100)
        Log("dl successfully")
        FTP.SendCommand("CWD", "..")
    Else
        Log("Error downloading")
        ToastMessageShow("DL error", True)
    End If
    FTP.Close
    Log("close")
 
    CallSubDelayed(Me, "DLpngSub_Complete")
End Sub

Private Sub Information_LongClick
    DLpngSub
    Wait For DLpngSub_Complete
 

    'File.Copy(File.DirAssets, personalid & ".png", xui.DefaultFolder, personalid & ".png")
    Log(File.ReadBytes(xui.DefaultFolder, "test.png"))
    If File.Exists(xui.DefaultFolder, "test.png") = True Then
        ImageView.SetBitmap(xui.LoadBitmapResize(xui.DefaultFolder, "test.png",ImageView.Width,ImageView.Height,True))
    End If
    PanelPic.BringToFront

End Sub

Not good? 🤔 I just want to be sure, the file has been downloaded.
 
Last edited:
Upvote 0

Mentalist

Member
Licensed User
Longtime User
A short app would not be a problem, but: How to hide the login for ftp, so you still can try it (it's a little bit too public here)? 🤔
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
We haven't known the problem is after downloading after some posts, maybe a clearer subject would be something like: "Problem showing a image in a image view after downloading from FTP" or something like that.

You can make a small test to upload with some of this resources

 
Upvote 0
Top