I can't open file on micro sd card

Use arduino with Linksprite JPEG Camera . i want save image to micro sd card but open file jpg. :sweat_smile:

#include <SoftwareSerial.h>
#include <SD.h>

byte incomingbyte;
SoftwareSerial mySerial(2,3);                     //Configure pin 4 and 5 as soft serial port
int a=0x0000,j=0,k=0,count=0,i=0,p=0;                    //Read Starting address       
uint8_t MH = 0x00,ML = 0x00;
boolean EndFlag=0,HeaderFlag=0;
int chipSelect = 4;
File logfile;
                               
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{ 
  Serial.begin(38400);
  mySerial.begin(38400);
  a-=0x20;
  
  
  //see if the card is present and can be initialized
  if (!SD.begin(chipSelect)) {
    Serial.print("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");

 // create a new file
  char filename[] = "Test10.jpg";
  for (uint8_t i = 0; i < 100; i++) {
    filename[4] = i/10 + '0';
    filename[5] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  Serial.print("Logging to: ");
  Serial.println(filename);
}
  

void loop() 
{
     Serial.println("about to take picture...");
     
     byte a[32];
     p=0;
     SendResetCmd();
     delay(4000);                               //After reset, wait 2-3 second to send take picture command
      
     SendTakePhotoCmd();

     while(mySerial.available()>0)
      {
        incomingbyte=mySerial.read();
      }   
        
      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))      //Check if the picture is over
               EndFlag=1;                           
               j++;
          count++;
               }
          }
         
          for(j=0;j<count;j++)
          {   if(a[j]<0x10)
              Serial.print("0");
              Serial.print(a[j], HEX);
              Serial.print(" ");
          }           //Send jpeg picture over the serial port
          
          if(((a[j-32]==0xFF)&&(a[j-31]==0xD8))||(p))
            {
            logfile.print((char*)a);
            Serial.println();
            p=1;
            
            }
      }
     logfile.close(); 
     Serial.print("Finished writing data to file");  
     while(1);    
}

//-----------------
//Camera functions

//Send Reset command
void SendResetCmd()
{
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x26);
      mySerial.write((byte)0x0);
}

void SetImageSizeCmd()
{
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x31);
      mySerial.write(0x05);
      mySerial.write(0x04);
      mySerial.write(0x01);
      mySerial.write((byte)0x0);
      mySerial.write(0x19);
      mySerial.write(0x22);
}

void SetBaudRateCmd()
{
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x24);
      mySerial.write(0x03);
      mySerial.write(0x01);
      mySerial.write(0xAE);
      mySerial.write(0xC8);

}


//Send take picture command
void SendTakePhotoCmd()
{
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x36);
      mySerial.write(0x01);
      mySerial.write((byte)0x0);  
}

//Read data
void SendReadDataCmd()
{
      MH=a/0x100;
      ML=a%0x100;
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x32);
      mySerial.write(0x0c);
      mySerial.write((byte)0x0); 
      mySerial.write(0x0a);
      mySerial.write((byte)0x0);
      mySerial.write((byte)0x0);
      mySerial.write(MH);
      mySerial.write(ML);   
      mySerial.write((byte)0x0);
      mySerial.write((byte)0x0);
      mySerial.write((byte)0x0);
      mySerial.write(0x20);
      mySerial.write((byte)0x0);  
      mySerial.write(0x0a);
      a+=0x20;                            //address increases 32£¬set according to buffer size
}

void StopTakePhotoCmd()
{
      mySerial.write(0x56);
      mySerial.write((byte)0x0);
      mySerial.write(0x36);
      mySerial.write(0x01);
      mySerial.write(0x03);        
}

The problem isn't that you can't open the file. The problem is that the file contains data that does NOT represent a JPEG file. So, your post title is wrong.

     delay(4000);                               //After reset, wait 2-3 second to send take picture command

Is 4000 2 or 3 seconds? Stupid comments need to at least be right.

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

I guess it doesn't matter that the camera might have told you to go take a flying leap.

         SendReadDataCmd();
         delay(20);
          while(mySerial.available()>0)
          {

The Arduino can read data far faster than it can arrive. This is completely the wrong approach to getting picture data from the camera. There is some end-of-packet marker that indicates that the end of the picture data has arrived. You need to keep reading data, and waiting as necessary, until that marker arrives.

          if(((a[j-32]==0xFF)&&(a[j-31]==0xD8))||(p))

Isyourspacekeybroken? If so, get it fixed. If not, use it!

            logfile.print((char*)a);

Not even in the same universe as the correct way to write binary data to the SD card.

Cross-posting won't make you any friends here, so I've deleted the duplicate.