Search code examples
androidurih.264fileinputstreamandroid-contentresolver

Get FileInputStream from Android content URI (or how to seek on an InputStream)


I have a video player app that plays custom h264 transport streams. I was tasked with being able to launch our player from methods such as the user opening "gallery", selecting "movies" and after selecting a movie picking our app to launch...this all works perfectly. My issue is in how to do seeking back and forth on an InputStream using ContentResolver.

I've read many many posts here as well, such as Android: Getting a file URI from a content URI? as well as a rant http://commonsware.com/blog/2013/08/07/for-android-apis-think-streams-not-files.html about using InputStream over other methods, but with large video files, skip and mark/reset will not work for seeking in our player, so a plain InputStream is kind of useless...a FileInputStream works great and as a hack I cast the resolver.getInputStream to a FileInputStream and it played ball, but that's a hack...so....how do I get a File object or FileInputStream from content uri? or how can i effectively seek on an input stream?


Solution

  • So while it is possible that a content URI might not be backed by a File, it is possible to determine if it is...You can use getStatSize from ParcelFileDescriptor and it will return a value if the backing asset is a file, -1 if not. If it is a file, then obviously you can get a FileInputStream and the underlying channel