0

I have an mp4 file that I've created. I want to make a copy of it with a different name.

Simple task. Indeed. Tried all the ways here but none of them worked well.

The simplest way is

    try {
        InputStream in = new FileInputStream(src);

        OutputStream out = new FileOutputStream(dest);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

After copying, I'm trying to run the video file. The original is working as usual, but the copy is not. Copied the files to my computer and checked for difference. I saw that the bytes are different in some places.

Tried to copy to different folders, different source files, different destination files, rebooting my device, re-install my APK... Don't know what to try anymore.

What am I doing wrong here?

Community
  • 1
  • 1
Raziza O
  • 1,560
  • 1
  • 17
  • 37
  • As noted in the comments there, this simple way potentially leaks open streams when an exception is thrown. `src` is a real file, not a socket or similar? No exceptions in the log? – dhke Aug 02 '16 at 12:10
  • Yeah, everything is simple as it is.. Nothing is raised and the files exists. i deleted them and ran again the copy.. and again.. the first 1024 are different in away that that file is not viewable anymore. – Raziza O Aug 02 '16 at 12:20
  • if you debug the app is there any chance of **len != non-null bytes count in the buf array** ? – Yazan Aug 02 '16 at 12:33
  • 1
    Didn't quite get you. But I've found out my problem.. grrr.. happens allot after i'm banging my head on the wall all day and then finally posting a question. The problem was that the file was not closed yet before i started to copy it. different threads. I will not delete this question just in case some else facing the same annoying problem – Raziza O Aug 02 '16 at 12:40
  • You can use copyFile function from [here](http://stackoverflow.com/a/20559197/1608643) – Ketan Ahir Aug 02 '16 at 12:54
  • 1
    @KetanAhir Please read the comments: that won't solve the problem with multiple threads. – m0skit0 Aug 02 '16 at 12:55
  • @RazizaO i meant to check if `len` value is (assume) 1024 where the `buf` array have last 10 - 20 bytes = null or 0, anyways it was something else :) – Yazan Aug 02 '16 at 12:56
  • 1
    And apparently (and it makes sense) the `MediaMuxer` is fixing the header of the file in the end after all video samples were written.. @KetanAhir First i've used this way of coping but I thought that this was the problem - So i went on the safe way with bytes copying.. Anyway I switched back to this code.. more elegant. – Raziza O Aug 02 '16 at 14:54

0 Answers0