Android Question Check if Bitmap File isn't corrupt

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi all,

I have a problem here... when downloading a bitmap, sometimes due poor internet connection the file is corrupted, but still written in filesystem like filename.png (or something like that...) . But when the app tries to load the file into a imageview, then get an error (many times an unrecoverable error that will obligate the user to delete data, what is not good)... the question:
is there any way to ensure that the file anything.png is real a bitmap and isn't corrupted before trying to load in a imageview (avoiding an error?)
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi all,

I have a problem here... when downloading a bitmap, sometimes due poor internet connection the file is corrupted, but still written in filesystem like filename.png (or something like that...) . But when the app tries to load the file into a imageview, then get an error (many times an unrecoverable error that will obligate the user to delete data, what is not good)... the question:
is there any way to ensure that the file anything.png is real a bitmap and isn't corrupted before trying to load in a imageview (avoiding an error?)
Probably your best bet would be to use a Try, Catch block to trap the error & take another action in the case of a corrupt file.

- Colin.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i you made a get request try to get the data length in http response header. it should equal the file length.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
i you made a get request try to get the data length in http response header. it should equal the file length.
This is an option but the best could be to have a method to confirm that the file signature is compatible with a non corrupt png archive... even because the file could be corrupted in server side also due the same reason (internet problem when uploading)
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
This might help point you in the right direction -> Inline Java Code
Hi! Of course!!! I already used a similar integration in my codes to do rounded images inside imageviews... I only didn't know that works with any java code without needing to do changes... thanks! I'll test!
(by the way: could be a good idea to have a check like this in the code job.getbitmap when downloading images from web... could add an extra program stability, specially when working with unstable networks and devices with low CPU/memory capacity that could generate an app fail during an image download...)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi! Of course!!! I already used a similar integration in my codes to do rounded images inside imageviews... I only didn't know that works with any java code without needing to do changes... thanks! I'll test!
(by the way: could be a good idea to have a check like this in the code job.getbitmap when downloading images from web... could add an extra program stability, specially when working with unstable networks and devices with low CPU/memory capacity that could generate an app fail during an image download...)

True - but it is also very easy to just use a Try, Catch block when loading the image into the ImageView (per my first reply). :)

- Colin.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
For two out of three cases (PNG and GIF), it just uses the try/catch block around the opening of the file to catch an error. The third case, JPEG, it checks the final two bytes. I guess the question is does B4X's image loading. Looks to me like that this could be done in plain B4A.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
For two out of three cases (PNG and GIF), it just uses the try/catch block around the opening of the file to catch an error. The third case, JPEG, it checks the final two bytes. I guess the question is does B4X's image loading. Looks to me like that this could be done in plain B4A.
This is a quite elegant solution without using external resources... I think that there is someway to open the file as bytes and check the values at some strategic positions to validate.
Notice that a corrupted image file can make an app unusable until user cleans data and starts it again (what could be a bad idea because involves many times the loose of the context of the app, maybe the user evolution in a game, maybe some important data... and forces the developer to preview a more intensive synchronization process in server due the high probability of "clean user data needing" events). So, the capacity to check and restore silently only one image in background has more implications in a good client server project.
Anybody knows how could be a code to check the correct bytes in a png/jpg/gif file using only B4A?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
there any way to ensure that the file anything.png is real a bitmap and isn't corrupted before trying to load in a imageview
Probably your best bet would be to use a Try, Catch block to trap the error & take another action in the case of a corrupt file.
I still think @Computersmith64's answer is the simplest solution. I tried this with a corrupted PNG, GIF and JPEG file. I just deleted some bytes out of both the PNG and GIF and removed some bytes from the end of the JPEG file. In all three cases, LoadBitmap threw an error that was caught with a try/catch block.

B4X:
Dim bmp As Bitmap
Try
   bmp = LoadBitmap(File.DirAssets, "file2.png") ' Using DirAssets and file2.png for testing purposes only
Catch
   log(LastException)
End Try
If bmp.IsInitialized = True Then
  ' Do whatever you need to do with a properly loaded image file
Else
  ' Do whatever you need to do when an image file fails to load
End If

If you want more compact code you could go the Sub route
B4X:
Dim bmp as Bitmap = SafelyLoadBitmap(File.DirAssets, "file2.png")
If bmp.IsInitialized = True Then
  ' Do whatever you need to do with a properly loaded image file
Else
  ' Do whatever you need to do when an image file fails to load
End If

Sub SafelyLoadBitmap(dir As String, fname As String) as Bitmap
   Dim b As Bitmap
   Try
      b = LoadBitmap(dir, fname)
   Catch
      log(LastException)
   End Try
   Return b
End Sub
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I still think @Computersmith64's answer is the simplest solution. I tried this with a corrupted PNG, GIF and JPEG file. I just deleted some bytes out of both the PNG and GIF and removed some bytes from the end of the JPEG file. In all three cases, LoadBitmap threw an error that was caught with a try/catch block.

B4X:
Dim bmp As Bitmap
Try
   bmp = LoadBitmap(File.DirAssets, "file2.png") ' Using DirAssets and file2.png for testing purposes only
Catch
   log(LastException)
End Try
If bmp.IsInitialized = True Then
  ' Do whatever you need to do with a properly loaded image file
Else
  ' Do whatever you need to do when an image file fails to load
End If

If you want more compact code you could go the Sub route
B4X:
Dim bmp as Bitmap = SafelyLoadBitmap(File.DirAssets, "file2.png")
If bmp.IsInitialized = True Then
  ' Do whatever you need to do with a properly loaded image file
Else
  ' Do whatever you need to do when an image file fails to load
End If

Sub SafelyLoadBitmap(dir As String, fname As String) as Bitmap
   Dim b As Bitmap
   Try
      b = LoadBitmap(dir, fname)
   Catch
      log(LastException)
   End Try
   Return b
End Sub

Hi @OliverA ... what elegant and easy solution! I'll test. Thanks
 
Upvote 0
Top