Android: create a file from android.net.URI

I have an android.net.URI object (the view returned by onActivityResult after MediaStore.ACTION_VIDEO_CAPTURE): it looks like content: // media / video / media / 33.

I want to convert this to a File object, and only a File object — I need to pass it to another constructor that needs a file.

How can I convert a URI like this to a File object? If i try

File newFile = new File(myURI);

I get an error in Eclipse asking me to convert a URI to a string. Supplying URI.getPath () in the constructor does not help either.

This applies to How to convert an android.net.uri object to a java.net.uri object? ?, unfortunately, unfortunately, the answer, but I need a File object, not a java uri.

I do not mind if I need to write it to the stream stream and vice versa - no matter what the best.

Sorry if I cross-post my own question , but I thought I might need to do something clearer.

+5
source share
1 answer

How can I convert a URI like this to a File object?

You cannot with a reliable SDK-compatible way.

“If the record in the table is the content: URI, you should never try to open and read the file directly (on the one hand, permission problems can fail). Instead, you should call ContentResolver.openInputStream () to get an InputStream object, which you can use to read data. "

( http://developer.android.com/guide/topics/providers/content-providers.html)

, InputStream. , , OutputStream, , , .

+6

All Articles