This is almost certainly because the file is still transferring when it is detected in the bluetooth folder. I'm assuming you have no scriptable control over the transfer of the file, ie. it is pushed from the smartphone not using any script. In that case what you could do is use inotifywait (from the inotify-tools package) to monitor the file-system events on the bluetooth folder and then if you see close_write (or create) events you could see if you could write a script to react on those close_write (or create) events rather than just looking at directory contents every so often.
Here's one way to script it:
- Create a named pipe to store the filenames to be processed.
- Run a command in the background to constantly read from the named pipe and process newly created files.
- Call inotifywait to react to create events on the bluetooth folder and then send the newly created files to the named pipe.
process_images < image_pipe &
inotifywait -mq -e create --format %f . > image_pipe
where your process_images function is something like:
while read filename; do