How to use sync() when writing to SD card (via SAF)

Asked 2 years ago, Updated 2 years ago, 41 views

I would like to copy the file from the internal storage to the SD card on the Android device.
SD cards are written via SAF.
I want to scan media immediately after writing, but due to the delay in writing to the SD card, I cannot load it properly.
For internal storage, sync() can be used to control write completion, but can sync() be used in SAF?
Use the code below, but it doesn't work as expected.

public classBinaryRename_sd{
    CountDownLatch countDownLatch;

    public boolean rename(DocumentFilepickedDir, String path, String new_path, ProgressDialog dialog, Context context) rows IOException {

        DocumentFile newFile=pickedDir.createFile(", new_path);
        for(int wait=0;wait<10;wait++){
            newFile=pickedDir.findFile(new_path);
            try{
                if(newFile!=null){
                    wait = 10;
                } else {
                    countDownLatch = new CountDownLatch(1);
                    new Thread (new Runnable() {
                        @ Override
                        public void run() {
                            try{
                                Thread.sleep(500);
                            } catch(InterruptedExceptione) {
                                e.printStackTrace();
                            }
                            countDownLatch.countDown();
                        }
                    }).start();

                    countDownLatch.away (510, TimeUnit.MILLISECONDS);
                }

            } catch(InterruptedExceptione) {
                e.printStackTrace();
            }
        }
        FileInputStream in = new FileInputStream (path);
        double fsize = in.available();

        OutputStream fos=context.getContentResolver().openOutputStream(newFile.getUri());

        try{
            intlen_body_read;
            int buf = 1024000*1;
            byte buf_body[] = new byte [buf];
            loaded = 50;
            double degree = 0.0;

            while((len_body_read=in.read(buf_body))!=-1){
                try{
                    fos.write(buf_body,0,len_body_read);

                    loaded = loaded + len_body_read;
                    degree=(loaded/1000*100)/(fsize/1000*2);

                    if(dialog!=null){
                        if((int)Math.round(degre)>95){
                            dialog.setProgress(95);
                        } else {
                            dialog.setProgress(int)Math.round(degre)+50);
                        }
                    }
                } catch(Exceptione){
                    e.printStackTrace();
                }
            }

            fos.flush();
            fos.close();

        }
        US>finally
            in.close();
        }

        File delFile=new File(path);
        delFile.delete();
        delFile=null;

        return true;

    }

android

2022-09-30 19:30

1 Answers

Flush() in FileOutputStream does not complete synchronization with the file system.
To complete synchronization with the file system at the intended time, you still need to run sync explicitly.

Try the following:

OReplace with OutputStream→FileOutputStream
(2) Run sync after flush

// Sample code
try{
    FileOutputStream fos=newFileOutputStream(newFile("any file"));
    fos.write(val);
    fos.flush();
    Explicitly synchronize with fos.getFD().sync();//←sync
    fos.close();    
} catch(Exception exception) {
}

If you run sync frequently, the performance will deteriorate, so you should run it once just before closing the file.


2022-09-30 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.