Im accessing a large file on a USB flash drive plugged into Android
That may be a problem.
Since the file is large, i need to "seek" inside the file rather than load in the whole thing.
That may be a bigger problem.
In C language, seek is just one line of code
Not necessarily.
Right now the code i have to access the file on the usb drive are
I am assuming that you are getting this Uri
from ACTION_OPEN_DOCUMENT
or some similar API tied into the Storage Access Framework. If so, you will have better luck if you think of the Uri
as being an actual URI. URIs, such as a URL, do not necessarily represent local resources and do not necessarily represent something for which "seek is just one line of code".
InputStream
offers mark()
and reset()
, which are the closest things that you have to true random seek capability. And, not every stream will support those — for example, you cannot necessarily reset()
a stream backed by a TCP/IP socket.
But how do i get a File Object from the ContentResolver
You don't, other than by using the InputStream
to read in the entire content represented by the Uri
, writing it out to some FileOutputStream
for a File
that you control (e.g., on internal storage via getFilesDir()
), and then using the resulting file.
Or, see if you can get by with mark()
and reset()
, also calling markSupported()
to see if that is possible with the InputStream
(it should be in your case, but I cannot guarantee it).
Or, establish a prerequisite that the file be copied by somebody else off the USB flash drive onto a portion of the device's on-board flash filesystem that you can directly access, such as external storage.
Or, establish a prerequisite that the file be located in a particular location on removable storage that your app has direct filesystem access to (i.e., getExternalFilesDirs()
, where the second and subsequent entries in the array are locations on removable media that you can read and write from).