Android Question Error Loading Bitmap


Licensed User
Hi, I have an App where the user subscribes to a service of his choice , and according to the service chosen a .png file is downloaded and used as an icon to represent the service he is subscribed to . Everything works fine and I have multiple logs that check that the download was executed correctly and that the file exists.
but I keep getting this error when trying to load the .png file ( which is downloaded to DirInternal ) to an Imageview .

** Activity (selectscreen) Pause, UserClosed = false **
** Activity (billlist) Create, isFirst = true **
DB size is:1
billlist_createitem (java line: 743)
java.lang.RuntimeException: Error loading bitmap.
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)
*** Service (starter) Create ***
** Service (starter) Start **

Just to make sure that the file is not corrupted If download the file and copy it to the app files folder before compiling, I can then load it into the imageview by calling the path from DirAssets, that works fine. But it's no use to me as there are multiple services the user can subscribe to , and there is no way of knowing which will be chosen. Hardcoding all the possible . png files into the source code would be a waste.

Here is the code that handles the imageview:

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
   Dim Path As String = File.DirInternal
    ImageView1.SetBitmap(xui.LoadBitmapResize(Path, Image, ImageView1.Width, ImageView1.Height, True))
    Return p
End Sub

I've tried everything, including moving the file to GetSafeDirDefaultExternal , but I still can't get this to work any help would be really appreciated .


Licensed User
Longtime User
Maybe the image is corrupt? Are you sure the image downloaded to dirInternal is OK. Not just existing...
Can you upload the image?
Upvote 0


Licensed User
Hi DonManfred, as I said in the first post I also tried downloading the icon file and placing it in the 'files' folder before compiling I can then modify the code to load the file from DirAssets instead of DirInternal , and that works just fine, so it seems that the file is not corrupted. Anyways I've tried different .png files and I always get the same error
Upvote 0


Licensed User
Ok sounds like a good idea. I'll create a small single activity app with an imageview and see if the problem persists
Upvote 0


Active Member
Licensed User
Longtime User
How you solved the problem?

i have the same problem on some devices from Android 5 to 7.1
Upvote 0


Licensed User
Hi Ferdari ,

It appears that you need to make sure that the code flow is paused until the download is completed, otherwise the code will carry on executing before the download is finished, which will lead to a crash as it tries to open an incomplete file. Simply checking if the file exists can be misleading because this can return True even if the download is not complete since the name is created before the download is finished.. As you can see I called "Wait For " twice in my code , that did the trick.
Anyways with the following code it now works for me :

 Dim IconLink As String = ""
 Dim checkFile As String = "myImage.png" 
If File.Exists(File.DirInternal, checkFile)  = True Then
    Log ( "File already exists")
Wait For (DownloadAndSaveIcon (IconLink,checkFile)) Complete ( Success As Boolean) 
        End If
    Sub DownloadAndSaveIcon (Link As String, Filename As String) As ResumableSub
    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
        Return J.Success
End Sub
Upvote 0