Android open file input stream from uri

First question here is why it displays a file named “servo.dat” as numbers (in this case it shows “5889”)?

Because it is not a file. It is a piece of content, and you are attempting to treat the path portion of a Uri as a filesystem path, which is is not.

If you want a display name for the content:

  • Call DocumentFile.fromSingleUri(), passing in your Uri, to create a DocumentFile
  • Call getDisplayName on the DocumentFile

Basically the path provided here is incorrect

That is because you are trying to treat the path portion of a Uri as a filesystem path, which it is not.

To get an InputStream, call openInputStream() on a ContentResolver, passing in your Uri. See the documentation. So, for example, from a method in an Activity, you would use InputStream inputStream = getContentResolver().openInputStream(uri);.

I’m getting confused since I’m new to both Android app development and Android itself

You may wish to consider reading a book on Android app development or taking a course in Android app development.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top