Help Uploading Photo to SD card

I am using the VC0706 Serial Camera to take a burst of photos when I detect motion. I have successfully coded the motion side of it, but I am having trouble uploading them to the SD card. I can run the code fine, but when I try to open the image file, I get the error " The file 'IMAGE75.JPG' could not be opened because it is empty." I am using these SD card writers, and here is my code:

#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>

#if defined(__AVR__) || defined(ESP8266)

#include <SoftwareSerial.h>         
SoftwareSerial cameraconnection(2, 3);

#else

#define cameraconnection Serial1

#endif

Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

#define chipSelect 10

void setup() {

#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega
#else
  if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

  Serial.begin(9600);
  Serial.println("VC0706 Camera snapshot test");
}

void loop() {
    if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }  
  
  // Try to locate the camera
  if (cam.begin()) {
    Serial.println("Camera Found:");
  } else {
    Serial.println("No camera found?");
    return;
  }

  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print("Failed to get version");
  } else {
    Serial.println("-----------------");
    Serial.print(reply);
    Serial.println("-----------------");
  }
  
  cam.setImageSize(VC0706_640x480);        // biggest
  //cam.setImageSize(VC0706_320x240);        // medium
  //cam.setImageSize(VC0706_160x120);          // small, change the resolution if we are taking too few photos, but remember it may not be worth it!

  uint8_t imgsize = cam.getImageSize();
  Serial.print("Image size: ");
  if (imgsize == VC0706_640x480) Serial.println("640x480");
  if (imgsize == VC0706_320x240) Serial.println("320x240");
  if (imgsize == VC0706_160x120) Serial.println("160x120");

  if (! cam.takePicture()) 
    Serial.println("Failed to snap!");
  else 
    Serial.println("Picture taken!");
  
  // Create an image with the name IMAGExx.JPG
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i/10;
    filename[6] = '0' + i%10;
    if (! SD.exists(filename)) {
      break;
    }
  }
  
  // Open the file for writing
  File imgFile = SD.open(filename, FILE_WRITE);

  // Get the size of the image (frame) taken  
  uint16_t jpglen = cam.frameLength();
  Serial.print("Storing ");
  Serial.print(jpglen, DEC);
  Serial.print(" byte image.");

  int32_t time = millis();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  byte wCount = 0; // For counting # of writes
  while (jpglen > 0) {
    uint8_t *buffer;
    uint8_t bytesToRead = min((uint16_t)32, jpglen); // if does not work with other setup, reduce until it works
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
      Serial.print('.');
      wCount = 0;
    }
    Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
    jpglen -= bytesToRead;
  }
  imgFile.close();

  time = millis() - time;
  Serial.println("done!");
  Serial.print(time); Serial.println(" ms elapsed");
}

Does anyone have any ideas what the problem is? Thanks!

I gave up looking at your code when I saw all the stuff in loop() that belongs in setup() so it executes only once when the program starts.
Paul

I purposefully put all of the code in loop() because I want the camera to take multiple photos, rather than taking one photo and then shutting off.