'Videos used in app keep getting corrupted after power loss on Android device

I have to write an app that displays videos and if someone plus in usb stick to the device, video is copied from it and played on the device. Pretty simple. I got it all working with USBBroadcastReceiver to copy te files, it all works as intended. It's also set as home app if that's important.

Only problem I have is that target devices have no battery, by that I mean, they have to be constantly plugged in (LCD Strips). Not much of a problem until power outage or somenthing like that.

Video I'm using (playing with VideoView) gets corrupted and can't be played anymore so someone would have to do a roud with usb stick and plug it into however many devices they would have. I've observed the same thing while playing it with just an gallery app.

I've thought I've had a quick fix by simply while copyig the file, I'd also make an backup one and then If the video is corrupted, just copy again from backup and problem solved.
Not so easy after all, backup get's corrupted too. I think this might have to do with this file being cached or something?
It's my first android app and basically first java app so I might not know something obvious.

Here are my copy methods, to me it looks like it shouldn't keep any data about the backup after flushing the stream, but somehow it does.

public void CopyFile() throws IOException {
        File vid1=new File(VideoFilePath);
        File vid2=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoclip.mp4");
        //File vid3=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoclip_backup.mp4");
        try {
            System.out.println("Copying file");
            copy(vid1, vid2);
            //copy(vid1, vid3);
            System.out.println("Finished copying file");
            IsBeingCopied = false;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed copying file");
        }
    }
    public void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dst);
            try {
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.flush();
                out.close();
            }
        } finally {
            in.close();
        }
    }

In Main, in onCompletition listener (which is there to loop the video) I just check if file is corrupted or couldn't be read and copy from the backup one so it's not even used there before after turning the device again.

Is there any way to fix this or sudden system shutdowns will always corrupt the files used in this app?

EDIT. I fixed it, I knew it was just some dumb mistake. All I had to do is save files of Movies directory, insted of just on Internal Storage. You live and learn.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source