How to build File Uri without newing a file?

I need to build a Uri object from a file path, but using Uri.fromFile(new File(path)) is too slow, so I want to build it manually.

First of all, Uri.parse("file://" + path) does not work, since it does not path-encode the path.

I tried Uri.Builder().scheme("file").path(orgPath).build(), but the result is: file:path instead of file://path.

How can I build a Uri as same as Uri.fromFile() does, in a faster way?

Thank you!

Upvotes: 1

Views: 317

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

try Uri.encode()

"file://"+Uri.encode(path)

or if you want to allow the string like / or any other than pass it as second parameter

like :

  "file://" + Uri.encode(path,"/")

Upvotes: 2

OK I find out I just need to add .auth(). Uri.Builder().scheme("file").auth("").path(orgPath).build() is working fine.

Upvotes: 2

Related Questions