Image Issue

Bill Norris

Active Member
Licensed User
Longtime User
My app is deployed and working fine, but occasional problem. Each activity has a background image, and every once in a while, I receive "Error loading bitmap". I'll go to file manager, and the image file is where it is supposed to be, but the icon next to it is the funny-looking default android icon. When I touch the file to display in gallery, it will be blank screen. If I re-download the image from my server, all is well. This is happening on random background files for no obvious reason. How can a file get "corrupted" or whatever is happening? There is no user interaction with the file, it just loads when the activity starts. Should I be "unloading" the background bitmap prior to closing the activity
 

Bill Norris

Active Member
Licensed User
Longtime User
RE:

No, they are in default.external and are loaded as needed. The file name of the background image is stored in database and is read on activity.create and then loads the file to background. I did it this way so the end user can change the background image if they want.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Yes -- they are all downloading successfully. The problem occurs randomly. The image files display successfully numerous times before they somehow get "corrupted" and cause the app to crash. I have written in try/catch blocks at each point where the background images load and that hopefully solves the crashing issue, but doesn't solve the file corruption issue.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Here is code used to download the image files from our web server. As mentioned earlier, this code functions as desired and all of the images download successfully and load successfully in the app when called. It is a random occurrence that a given image, after being used in the app successfully for several day, will suddenly fail to load when called and generates an error.

Job2 is called to download images

B4X:
Sub JobDone (Job As String)

   Select Job
      Case "Job1"  'database file only
         Dim FileUrl As String
         FileUrl=HttpUtils.tasks.Get(0)
         If HttpUtils.IsSuccess(FileUrl) Then 
            Dim database As InputStream
            database=HttpUtils.GetInputStream(FileUrl)
            Dim out As OutputStream
            Main.sql_main.Close
            out = File.OpenOutput(File.DirDefaultExternal,"WINE_TABLET.DB",False)
            File.Copy2(HttpUtils.GetInputStream(FileUrl), out)
            out.Close
            ToastMessageShow("Database Download Complete", True)
            Main.sql_main.Initialize(File.DirDefaultExternal,"WINE_TABLET.DB",False)
         End If
      Case "Job2"   'all image files
         Log ("Job2 Started")
         Dim link As String
         Dim file_name As String
         Dim file_path As String
         Dim file_plus_path As String
         Dim out As OutputStream
         
         'on first run, create appropriate directories
         Dim path_test As String
         path_test=File.DirDefaultExternal & "/images/themes"
         If Not(File.Exists(path_test,"")) Then 'if this folder exists, then the others should exist also
            File.MakeDir(File.DirDefaultExternal,"images/themes")
            File.MakeDir(File.DirDefaultExternal,"images/wine")
            File.MakeDir(File.DirDefaultExternal,"images/beer")
            File.MakeDir(File.DirDefaultExternal,"images/liquor")
         End If
   
         For i = 0 To HttpUtils.Tasks.Size - 1
              link = HttpUtils.Tasks.Get(i)
            Log(link)
            If HttpUtils.IsSuccess(link) Then
               file_plus_path=link.SubString(39)
               file_name=file_plus_path.SubString(file_plus_path.LastIndexOf("/")+1)
               file_path=file_plus_path.SubString2(0,file_plus_path.LastIndexOf("/"))
               Try 'in case a file is not found
                  out = File.OpenOutput(File.DirDefaultExternal & "/" & file_path, file_name,False)
                  File.Copy2(HttpUtils.GetInputStream(link), out)
                  out.Close
               Catch
               End Try
            End If
         Next   
         Log("Image Download Complete")
         ToastMessageShow("Image Download Complete", True)
         
      Case "Job3"   'new APK file
         Dim FileUrl As String
         FileUrl=HttpUtils.tasks.Get(0)
         If HttpUtils.IsSuccess(FileUrl) Then 
            Dim apk_file As InputStream
            apk_file=HttpUtils.GetInputStream(FileUrl)
            Dim out As OutputStream
            out = File.OpenOutput(File.DirRootExternal,"temp.apk",False)
            File.Copy2(HttpUtils.GetInputStream(FileUrl), out)
            out.Close   
            Dim Intent1 As Intent
            Intent1.Initialize(Intent1.ACTION_VIEW, "file:///sdcard/temp.apk")
            Intent1.SetType("application/vnd.android.package-archive")
            StartActivity(Intent1)

         End If
      
      
      
   End Select
End Sub
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

If you could give me a code snippet to log the error message I will put it in after the Catch. If I recall, the error is Error Loading Bitmap.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Thanks, -- BTW, I have noticed that the problem files are always either .gif or .png -- have not had the issue with .jpg -- don't know if that sheds any light.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
If you could give me a code snippet to log the error message I will put it in after the Catch. If I recall, the error is Error Loading Bitmap.

That looks like an Out of Memory. "Error loading bitmap" is the typical message I get when my device is out of memory. You should check the free memory with this function:
B4X:
Sub GetFreeMem As Float
   Dim r As Reflector
   Dim MM, TM, FM, Total As Int
   r.Target = r.RunStaticMethod("java.lang.Runtime", "getRuntime", Null, Null)
   MM = r.RunMethod("maxMemory")
   FM = r.RunMethod("freeMemory")
   TM = r.RunMethod("totalMemory")
   Total = MM + FM - TM - r.RunStaticMethod("android.os.Debug", "getNativeHeapAllocatedSize", Null, Null)
   Return Total / 1024
End Sub
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

That is good information to have, but I do not believe this is memory related. The files load fine for a while (several days or even weeks) the suddenly one will take a crap and give an error -- that file will then no longer even come up in Gallery -- just shows a blank screen. If I download a fresh copy of the image file, all is well for the time being.
 
Upvote 0
Top