1

I need to obtain a File object from a URI, working in Java, but keep getting a length of zero - though I know the file size is not zero.

I need the File object to pass to another constructor.

I'm not sure if it's because I'm constructing it in the wrong way? Here's my code:

    File videoFile = new File(videoURI.getPath());
    if (videoFile == null) {
        Log.d(LOG_TAG, "File not found!");
        return false;
    }

    Log.d(LOG_TAG, "about to upload, filepath: " + videoFile.getPath());

    Log.d(LOG_TAG, "File length: " + String.valueOf(videoFile.length()));

The log output doesn't spit out 'File not found!', and prints a non-null path, but shows a length of 0.

AP257
  • 89,519
  • 86
  • 202
  • 261
  • What happens if you try to open it with a FileInputStream? http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileInputStream.html – ehdv Feb 28 '10 at 18:48
  • Alas, I really need to get a File object out (to pass to another constructor, a MediaFileSource: http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/media/MediaFileSource.html). – AP257 Feb 28 '10 at 18:51
  • What's odd is that the Java definition suggests you can create a File from a URI alone - http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#constructor_summary - but I don't seem to be able to do File videoFile = new File(videoURI) without errors. – AP257 Feb 28 '10 at 18:53
  • videoFile will never be null even if the file doesn't exist. "new" will never ever ever return null... ever. If you want to see if the file exists you should ask the file object: videoFile.exists() Regarding the other issue, what errors do you get when you call new File(viewURI) directly? Compile errors or runtime errors? – PSpeed Feb 28 '10 at 20:41

4 Answers4

0

I had fought it, too, for 4 hours. A machine-independent solution is found:

File mFile = new File(new Path(mUri.getPath()).toString);

The mFile will have correct path in win/linux/mac and can work normally.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
0

Make sure your URI points to a file.

The videoFile will not be null because you might be creating a new file. So videoFile doesn't represent the actual file you think and that's why you get the 0 length.

File videoFile = new File(videoURI.getPath()); // videoFile != null

Try using the File constructor that takes in a URI.

marklai
  • 2,030
  • 1
  • 14
  • 14
0

If the file exists, but its length is "unexpectingly" zero, then chances are big that you created/wrote this file beforehand in the same program (or another program running in the same context), but didn't close its OutputStream. The normal Java IO idiom is to close streams after use. Also see this Sun tutorial for an example.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

One important thing to note is that the file object you get from

File videoFile = new File(videoURI.getPath());

is never going to be null, as with any object you get back from a constructor call. The only way you don't get an object back from a constructor is if the constructor throws an exception. So null checking the return as a check to see if the file exists is really doing nothing for you, if you really want to know if the file exits or not you should use videoFile.exists() instead. The File constructor that takes a URI directly is probably going to be a better option for you as it will perform all the necessary checking to ensure that the uri can be used as a file and will correctly extract the file path from the uri.

Yanamon
  • 630
  • 3
  • 11