The file uri is known, such as
`file:///mnt/sdcard/Download/AppSearch_2213333_60.apk`
I want to check if this file can open or not in background, how to do?
file. Files; boolean exists = Files. exists(Paths. get(URI));
A URI is a uniform resource identifier while a URL is a uniform resource locator.
To get File Uri from a absolute path of File you can use DocumentFile. fromFile(new File(path, name)), it's added in Api 22, and returns null for versions below. Show activity on this post. Uri uri = Uri.
Check if a file of a path exists like this:
File file = new File("/mnt/sdcard/Download/AppSearch_2213333_60.apk" );
if (file.exists()) {
//Do something
}
Keep in mind to remove something like "file://" etc. otherwise use:
File file = new File(URI.create("file:///mnt/sdcard/Download/AppSearch_2213333_60.apk").getPath());
if (file.exists()) {
//Do something
}
Also you have to set proper permissions for your app in the AndroidManifest.xml to access the sdcard:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With