Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a known uri file exists in Android storage?

Tags:

java

android

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?

like image 431
Victor S Avatar asked Jul 03 '13 07:07

Victor S


People also ask

How do I check if URI exists?

file. Files; boolean exists = Files. exists(Paths. get(URI));

What is the URI in Android?

A URI is a uniform resource identifier while a URL is a uniform resource locator.

How get URI from Filepath?

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.


1 Answers

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" />
like image 69
alex Avatar answered Sep 29 '22 10:09

alex