1. Are you ready for the Galaxy S20? Here is everything we know so far!

Weird "java.io.FileNotFoundException:" error

Discussion in 'Android Development' started by ar792, Jan 28, 2016.

  1. ar792

    ar792 Newbie
    Thread Starter

    In my video recording code, to implement pause functionality, 'n' no. of mp4 files are created in sequence.
    (file1.mp4, file2.mp4, file3.mp4.....)

    Following code is used for file name sequence; (thanks to Robpuk38):
    http://androidforums.com/threads/writing-filename-in-sequence.985745/

    After this, these 'n' no of files are parsed using mp4Parser:
    Code (Text):
    1.  
    2. public class MergeVide extends AsyncTask<String, Integer, String> {
    3.         File files;
    4.  
    5.         @Override
    6.         protected void onPreExecute() {
    7.          
    8.  
    9.             files = new File("/storage/emulated/0//Videos/Temp/");
    10.             File[] list = files.listFiles();
    11.             for (File aList : list) {
    12.                 System.out.println(aList + " piece of video");
    13.             }
    14.  
    15.  
    16.  
    17.         }
    18.  
    19.      
    20.  
    21.         @Override
    22.         protected String doInBackground(String... params) {
    23.  
    24.             String[] videoUris = new String[]{
    25.  
    26.                     Arrays.toString(files.listFiles()),
    27.  
    28.             };
    29.  
    30.      
    31.             try {
    32.                 List<Movie> inMovies = new ArrayList<Movie>();
    33.                 for (String videoUri : videoUris) {
    34.                     inMovies.add(MovieCreator.build(videoUri));
    35.                 }
    36.                 List<Track> videoTracks = new LinkedList<Track>();
    37.                 List<Track> audioTracks = new LinkedList<Track>();
    38.                 for (Movie m : inMovies) {
    39.                     for (Track t : m.getTracks()) {
    40.                         if (t.getHandler().equals("soun")) {
    41.                             audioTracks.add(t);
    42.                         }
    43.                         if (t.getHandler().equals("vide")) {
    44.                             videoTracks.add(t);
    45.                         }
    46.                     }
    47.                 }
    48.                 Movie result = new Movie();
    49.                 if (audioTracks.size() > 0) {
    50.                     result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    51.                 }
    52.                 if (videoTracks.size() > 0) {
    53.                     result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
    54.                 }
    55.                 Container out = new DefaultMp4Builder().build(result);
    56.                 FileChannel fc = new RandomAccessFile(String.format("/storage/emulated/0//output.mp4"), "rw").getChannel();
    57.                 out.writeContainer(fc);
    58.                 fc.close();
    59.             } catch (FileNotFoundException e) {
    60.                 e.printStackTrace();
    61.             } catch (IOException e) {
    62.                 e.printStackTrace();
    63.             }
    64.  
    65.  
    66.  
    67.             File oldFile = new File("/storage/emulated/0//Videos/output.mp4");
    68.             mFileName = new File("/storage/emulated/0//Videos/ video.mp4");
    69.             boolean success = oldFile .renameTo(mFileName );
    70.  
    71.             if(success)
    72.                 System.out.println("file is renamed..");
    73.             return String.valueOf(mFileName);
    74.         }

    All the recorded 'n' of file.mp4 are listed in the console, but I also get "java.io.FileNotFoundException:" for the same files!

    CONSOLE:

    /com.exapps.videorec I/System.out﹕ /storage/emulated/0/Videos/Temp/file1.mp4 piece of video
    /com.exapps.videorec I/System.out﹕ /storage/emulated/0/Videos/Temp/file2.mp4 piece of video
    /com.exapps.videorec I/System.out﹕ /storage/emulated/0/Videos/Temp/file3.mp4 piece of video
    /com.exapps.videorec W/System.err﹕ java.io.FileNotFoundException: [/storage/emulated/0/Videos/Temp/file1.mp4, /storage/emulated/0/Videos/Temp/file2.mp4, /storage/emulated/0/Videos/Temp/file3.mp4]: open failed: ENOENT (No such file or directory)
    /com.exapps.videorec W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
    /com.exapps.videorec W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
    /com.exapps.videorec W/System.err﹕ at com.googlecode.mp4parser.FileDataSourceImpl.<init>(FileDataSourceImpl.java:19)
    .................

    But if i set the above code as:
    Code (Text):
    1.  
    2. String[] videoUris = new String[]{
    3.  
    4.         /storage/emulated/0/Videos/Temp/file1.mp4,
    5.         /storage/emulated/0/Videos/Temp/file2.mp4,
    6.         /storage/emulated/0/Videos/Temp/file3.mp4
    7.  
    8. };
    It does work perfectly..

    Any help appreciated..!

     


  2. Best Answer:
    Post #8 by Deleted User, Jan 29, 2016 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. scary alien

    scary alien not really so scary
    VIP Member

    I wouldn't think that this would/should cause the issue that you're seeing (since it doesn't / won't from a Linux/Unix shell prompt), but do you see that you have a "//"(double slashes in the pathname list) in this line of code:

    files = new File("/storage/emulated/0//Videos/Temp/");

    right before you do the file list:

    File[] list = files.listFiles();

    See if removing the extra forward slash fixes things.

    Cheers!
     
  4. Deleted User

    Deleted User Guest

    Good spot Alien, but I reckon here is the problem - this piece of code will not create an array of individual Strings. It will create an array of one String, which has all the file names concatenated

    Code (Text):
    1. String[] videoUris = new String[]{
    2.                     Arrays.toString(files.listFiles()),
    3.             };
    The variable videoUris isn't actually needed. The code can iterate over files instead

    Code (Text):
    1.  
    2.            for (File file: files) {
    3.                     inMovies.add(MovieCreator.build(file.getPath()));
    4.            }
    5.  
     
    scary alien likes this.
  5. scary alien

    scary alien not really so scary
    VIP Member

    Cheers, @LV426--I'm sure you're right!

    I had a bit of doubt re. my simple suggestion...couldn't test it properly, but wanted to stall the OP a bit until you got here :).
     
    Deleted User likes this.
  6. Deleted User

    Deleted User Guest

    Hehe! What a team!
     
    scary alien likes this.
  7. ar792

    ar792 Newbie
    Thread Starter

    Thanks..
    The slashes aren't the issue, i have tried using a single one... the result is the same ... I'll give a try to LV426's solution,..!
     
    scary alien likes this.
  8. ar792

    ar792 Newbie
    Thread Starter

    I am using Android Studio..

    It gives an error: foreach cannot be applied to type 'java.io.File'
     
  9. Deleted User

    Deleted User Guest

    Ok you see that variable called 'list' in your onPreExecute() method? Make that a class variable, and do

    Code (Text):
    1.  
    2. for (File file: list) {
    3.                     inMovies.add(MovieCreator.build(file.getPath()));
    4.            }
    5.  
    Then read about Java enhanced for loop iteration if you don't fully understand what's going on here:

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
     
    scary alien and ar792 like this.
Loading...

Share This Page

Loading...