Android Question Error Loading Bitmap

Polaris

Member
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(CanvasWrapper.java:637)
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:743)
at simbills.beta.billlist._activity_create(billlist.java:432)
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)
*** 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:


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
   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 .
 

DonManfred

Expert
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

Polaris

Member
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

Polaris

Member
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

Ferdari

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

Polaris

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


B4X:
 Dim IconLink As String = "www.Put_Your_Url_Here.com"
 Dim checkFile As String = "myImage.png" 
If File.Exists(File.DirInternal, checkFile)  = True Then
    Log ( "File already exists")
                    Return
            Else
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)
    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
        j.Release
        Return J.Success
        
End Sub
 
Upvote 0
Top