Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists in a directory in sd card

I want to check whether a given file exists in android sd card. I am trying it out with creating a file using the absolute path and checking with file.exists() but its not working. The URL for the file is "file:///mnt/sdcard/book1/page2.html" and the file does exist. But somehow file.exists() isn't showing the same.

like image 480
working Avatar asked Jul 02 '12 11:07

working


2 Answers

File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");

if(myFile.exists()){
    ...
}

This should work.

like image 151
Adam Monos Avatar answered Oct 22 '22 23:10

Adam Monos


Try like this:

File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
    /*...*/
}

Also make sure you have:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

in your manifest file.

like image 45
Caner Avatar answered Oct 22 '22 23:10

Caner