Android Question JPEG to file pauses UI

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,

*** see update below ***

I'm writing a JPEG to a file during a picture_taken event. The image is 3264 x 1836, the format is JPEG.

The file ends up being a little under 1 MB in size and requires a little under one second to write to file. Changing the image size, quality, or format is not an option – in fact the image size will only get bigger.

I'm using the following code:

B4X:
  Dim ops As OutputStream = File.OpenOutput(dir, fn, False)
  bm.WriteToStream(ops, Qual, fmt)
  ops.Close

The problem is writing to file pauses the UI. I have the UI updating a status label and the one second pause doesn't look very good, although it's not a biggie.

Is there a way to move the bm.WriteToStream to execute in a background thread or something so that it doesn't pause the UI?

I haven't found anything in the forums. Can someone point to something or suggest a solution?

Thanks,
Barry.

Update:
From timing measurements I see that all the time is being taken during the conversion from bitmap to JPEG, not writing the JPEG data to file.

In my picture_taken subroutine the image data is received as a bitmap, I convert to a JPEG, then write to file.

I believe there is a way to receive JPEG format data directly from the camera in the picture_taken subroutine. I'll have to look at that.

My initial thought that it would speed things up by implementing the file write in the background is not correct.
 
Last edited:

canalrun

Well-Known Member
Licensed User
Longtime User
Hi @canalrun,
You should look into the Thread Library.

https://www.b4x.com/android/forum/threads/threading-library.6775/#content

We do the canal run on bikes, around a 30 miles round trip ;)

Thanks. You have me envious. I have read about your late night "going for a bike ride" posts.

The segment of the Erie Canal (central New York State, USA. Google it, it's interesting.) near me is only about 5 miles long. I got the name CanalRun back in the early 1990s. I used to run 5K's on the Erie Canal every day. One day I was thinking, "What name should I choose? I know, CanalRun.".

Barry.
 
Upvote 0
Top