Convert file path to a file URI?
The System.Uri
constructor has the ability to parse full file paths and turn them into URI style paths. So you can just do the following:
var uri = new System.Uri("c:\\foo");
var converted = uri.AbsoluteUri;
Convert a file path to Uri in Android
Please try the following code
Uri.fromFile(new File("/sdcard/sample.jpg"))
Convert file: Uri to File in Android
What you want is...
new File(uri.getPath());
... and not...
new File(uri.toString());
Notes
- For an
android.net.Uri
object which is nameduri
and created exactly as in the question,uri.toString()
returns aString
in the format"file:///mnt/sdcard/myPicture.jpg"
, whereasuri.getPath()
returns aString
in the format"/mnt/sdcard/myPicture.jpg"
. - I understand that there are nuances to file storage in Android. My intention in this answer is to answer exactly what the questioner asked and not to get into the nuances.
Android - Convert file path to uri (not working)
Try to use this below code
Uri uri = Uri.fromFile(new File(filepath));
Converting file path to URI
require 'uri'
uri = URI.join('file:///', '/home/user/dir1/dir2/dir3/name.ext')
=> #<URI::Generic:0x0000000263fcc0 URL:file:/home/user/dir1/dir2/dir3/name.ext>
uri.scheme
=> "file"
uri.path
=> "/home/user/dir1/dir2/dir3/name.ext"
uri.to_s
=> "file:/home/user/dir1/dir2/dir3/name.ext"
How to convert a Java File URI inot a absolute path?
check this -> Convert URL to AbsolutePath
Path p = Paths.get(url.toURI());
and how say @GOTO 0
Another option for those who use Java 11 or later:
Path path = Path.of(url.toURI()); or as a string:
String path = Path.of(url.toURI()).toString(); Both methods above
throw a URISyntaxException that can be safely ignored if the URL is
guaranteed to be a file URL.
UPDATE
And i found this -> https://eed3si9n.com/encoding-file-path-as-URI-reference/
The solution to the problem is described in great detail here
public static void main(String[] args) throws URISyntaxException
{
URI uri = new URI("file:/C:/a.txt");
save(uri);
uri = new URI("file:///C:/a.txt");
save(uri);
uri = new URI("file:///home/user/a.txt");
save(uri);
}
public static void save(URI targetURI)
{
Path p = Paths.get(targetURI);
String absolutePath = p.toString();
System.out.println(absolutePath);
}
Related Topics
How to Embed an Application Manifest into an Application Using VS2008
C# Windows Forms Application - Updating Gui from Another Thread and Class
Create SQLite Database and Table
How to Get the Directory from a File's Full Path
Limiting Double to 3 Decimal Places
How to Fix the Memory Leak in Ie Webbrowser Control
Using Contains() in Linq to SQL
A C# Equivalent of C's Fread File I/O
For Loop Not Returning Expected Value - C# - Blazor
MVC Web API: No 'Access-Control-Allow-Origin' Header Is Present on the Requested Resource
How to Return a Specific Status Code and No Contents from Controller
How to Transfer Authentication from Webbrowser to Webrequest
Adding Unknown (At Design Time) Properties to an Expandoobject