SD card not saving complete data

Using the Arduino Uno and DFRobot SD module and LinkSprite JPEG TTL camera, I am taking a picture and saving it to the SD card. The data is shown in HEX in the serial monitor. Everything that is printed to the serial monitor is not saving to the SD card. Is there a problem with the code, the SD card, or the capabilities of the Uno?

SD: 2GB PNY MicroSD

snippet of code where the problem lies:

void setup()
{ 
  Serial.begin(9600);
  mySerial.begin(38400);
  pinMode(13,OUTPUT);       //pin 2 is connected to LED to indicate the status of SD card initialization
  if(!sd_raw_init())
 {
    digitalWrite(13,HIGH);
    while(1);     
 }    
 TestFile.initialize(); 
 Serial.println("initializing........");
 TestFile.create_file("image11.jpg");     // create a new jpeg file
}
void loop() 
{      
  byte a[32];
     SendResetCmd();
     delay(3000);                            //Wait 1 second to send take picture command
      
     SendTakePhotoCmd();
     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }   
     TestFile.open(); 
      while(!EndFlag)
      {  
         j=0;
         k=0;
         count=0;
         SendReadDataCmd();
         delay(20);
          while(mySerial.available()>0)
          {
               incomingbyte=mySerial.read();
               k++;
               if((k>5)&&(j<32)&&(!EndFlag))
               {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
               EndFlag=1;                           
               j++;
        count++;
               }
          }
         
        for(j=0; j<count; j++)
        {   if(a[j]<0x10)
         Serial.print("0");
        Serial.print(a[j],HEX);           // observe the image through serial port
         Serial.print(" ");
       }
       
          TestFile.write((char*)a);    
          Serial.println();
     
         i++;
      
      } 
    TestFile.close();   
   Serial.print("Finished writing data to file");  
 
     while(1);
}

I expect that this function:

 TestFile.write((char*)a);

expects a character string and so only writes until it reaches a null character. It seems likely that a JPEG image is likely to contain some null characters.

     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }

This piece of code will flush the serial buffer and copy the last received byte in incomingByte.... so your missing bytes .....

  pinMode(13,OUTPUT);       //pin 2 is connected to LED to indicate the status of SD card initialization

If pin 2 is connected to the LED, why are you setting the mode for pin 13?

     delay(3000);                            //Wait 1 second to send take picture command

Is your watch slow?

If you can't make the obvious comments correct, do they serve any useful purpose? If not, why are they there?