Arduino mega2560 and bluetooth module HC-06 data loss problem

Hi, I'm korean student.

I'm working on my project that image file transfer between the mega2560 using bluetooth module (HC-06).
Image file format is RAW format.

void loop()
{
  datafile= SD.open("/PICTURE/1234.RAW",FILE_WRITE); // "1234.RAW" file create on PICTURE folder in sdcard 
  
  int i=0;
  if(Serial1.available())
  {
    while(1) 
    {
      
           byte inbyte_char= Serial1.read();
                
            Serial.print(inbyte_char); //data transfer check

            if((char)inbyte_char == '{') // start check
            {
            }
            else if((char)inbyte_char == '}') // end check
            {
              break;
            }
            else 
            {
               datafile.write(inbyte_char); //
         
            }  
            if( (i%50)==0) { //cutting 50 row.

              Serial.println("");
            }
            i++;
           
            delay(1);
      }
      
      Serial.println("Read success!");
    }
    delay(10);
    WalkDirectory(datafile); // LCD display
   datafile.close();
     
  }

In the code above, due to "Serial.print(inbyte_char)", byte data is output to the serial monitor.
However, due to "Serial.println("Read success!");" in WHILE sentence, "Read success!" also is output in the middle.
It seems that bluetooth connection is unstable in the middle and data is lost. So, I want to know how to prevent data loss during data transfer using the bluetooth. Please give me advise.??

Please give me advise.

Post ALL of your code.

There is no reason to have delay(1) in the while loop. Read the data that is available NOW, not later.

// Arduino SD image viewer
// Written by Stanley Huang stanleyhuangyc@gmail.com
//
// This program requires the UTFT library.
//

#include "UTFT1.h"
#include <SD.h>
#include<sd_raw.h>
#include<sd_reader_config.h>
#include<sd_raw_config.h>
//#include<SoftwareSerial.h>

// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

// for Arduino 2009/Uno
//UTFT myGLCD(ITDB28,19,18,17,16);   // Remember to change the model parameter to suit your display module!

// for Arduino Mega
UTFT myGLCD(ITDB50,38,39,40,41);   // Remember to change the model parameter to suit your display module!

#define SD_PIN 53

File root;
File datafile;
//byte inbyte;
byte inbyte_char;
char inchar;

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 480

//SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void ShowMessage(const char* msg1, const char* msg2 = 0)
{
  myGLCD.setFont(BigFont);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(0, 0,0 );
  myGLCD.print(msg1, CENTER, 195);
  if (msg2) myGLCD.print(msg2, CENTER, 220);
}

void LoadImage(File& file)
{
  for (int y = 0; y < SCREEN_HEIGHT && file.available(); y++) {
    uint16_t buf[SCREEN_WIDTH];
    for (int x = SCREEN_WIDTH - 1; x >= 0; x--) {
      byte h = file.read();
      byte l = file.read();
      buf[x] = ((uint16_t)h << 8) | l;
    }
    myGLCD.drawPixelLine(0, y, SCREEN_WIDTH, buf);
    
  }
}

void WalkDirectory(File dir)
{
  for (;;) {
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    if (entry.isDirectory()) {
      WalkDirectory(entry);
    } 
    else {
      ShowMessage("Loading image from SD card", entry.name());
      LoadImage(entry);
      myGLCD.setFont(BigFont);
      myGLCD.print("Next",CENTER,210);
      
    }
    delay(2000);
     myGLCD.clrScr();
    entry.close();
    // delay for a while between each image
    //delay(2000);
  }
}

/*void readDisk()
 {
 byte low;
 byte high;
 byte info[2];
 int i;
 int  result;
 Serial.println();
 //info[10]=_inbyte;
 for(i=0;i<50;i=i+2)
 {
 sd_raw_read(i,info,2);
 
 //Serial.print(info[0],DEC);
 //Serial.print(" ");
 //Serial.print(info[1],DEC);
 low=info[0];
 high=info[1];
 result=high<<8;
 //result<<8;
 Serial.print(" ");
 Serial.print(result+low,DEC);
 Serial.print(" ");
 }
 
 }*/

void return_message(){
  Serial.println("Reading...");
}


  
void setup()
{

  Serial.begin(9600);
  Serial1.begin(9600); //hardware Serial use. //pin18,19
  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  myGLCD.fillScr(0, 0, 0);

  

/*  if(!sd_raw_init())
  {
    Serial.println("MMC/SD initialization failed");      
  }*/
  pinMode(SD_PIN, OUTPUT);
  if (!SD.begin(SD_PIN)) {
    ShowMessage("SD not ready");
    return;
  }

  //delay(10);
 // root = SD.open("/PICTURE");
  //WalkDirectory(root);

    //ShowMessage("That's the end of the show", "Press RESET to start over");
}

void loop()
{
  datafile= SD.open("/PICTURE/1234.RAW",FILE_WRITE);
  //Serial.println("File write!!");
  byte info[2];
  int i=1;
  if(Serial1.available())
  {
    while(1) 
    {
      
           inbyte_char= Serial1.read();
                
            Serial.print(inbyte_char);
            if((char)inbyte_char == '{')
            {
            }
            else if((char)inbyte_char == '}')
            {
              break;
            }
            else 
            {
               datafile.write(inbyte_char);
         
            }  
            if( (i%50)==0) { //cutting 50 row.

              Serial.println("");
            }
            i++;
           
            delay(1);
      }
      
      Serial.println("Read success!");
    }
    delay(10);
    WalkDirectory(datafile);
   datafile.close();
     
  }
  /*
     if(Serial.available()) {
   while(Serial.available()>0) {
   Serial1.write(Serial.read());
   delay(2);
   }
   }*/
  //WalkDirectory(root);

Here is All code.

  Serial.begin(9600);
  Serial1.begin(9600); //hardware Serial use. //pin18,19

Is that the correct rate for your bluetooth device? Most operate at much higher speeds than than.

You definitely want to be communicating with the PC faster than that.