Java: creating a file from a URI?

I need to get a File object from a URI that runs in Java, but keep getting zero - although I know the file size is not zero.

I need a File object to go to another constructor .

I'm not sure because I'm building it wrong? Here is 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()));

Log output does not give "File not found!" and prints a nonzero path, but shows a length of 0.

+1
source share
4 answers

Make sure your URI points to the file.

VideoFile will not be empty because you can create a new file. So videoFile is not the actual file that you think and why you are getting a length of 0.

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

, URI.

0

, "" , , / ( , ), OutputStream. Java IO- . . Sun .

0

, ,

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

, , . , , - , . , , , , , , , videoFile.exists(). , URI , , , , , uri uri.

0

I also fought with him for 4 hours. Found a machine-independent solution:

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

mFile will have the correct path in win / linux / mac and may work fine.

0
source

All Articles