Jump to content
TurboMagic

Android JInputStream wrapper?

Recommended Posts

Hello,

 

since Android 10 we've got this nasty problem of no longer having direct file access

to such folders like TPath.GetSharedDownloadsDir.

 

Now I have managed via some intent call to either ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT
to display some file chooser and when the user selects one I get the Uri of the selected file.

With that I can get at an JInputStream and could read out the individual bytes of that stream.

 

But: how to get at the file size properly to know when to stop reading?

The Available method should, as per Android's documentation

https://developer.android.com/reference/java/io/InputStream#available() rather not be used.


Is there any "wraper" available which properly/nicely wrapt this in a TFileStream or something similar?

A quick search didn't turn up anything yet.


TurboMagic

Share this post


Link to post
3 hours ago, TurboMagic said:

With that I can get at an JInputStream and could read out the individual bytes of that stream.

 

But: how to get at the file size properly to know when to stop reading?

You don't need to know the file size for that.  Simply stop reading when the stream's various read() methods return -1 indicating EOF has been reached.

 

But, if you absolutely did need to know a file's size, then use the provided Uri to open a JFile to the file, and then you can call its length() method.  You can create a JInputStream for a JFile using Android's FileInputStream class.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×