Question

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.

Was it helpful?

Solution

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

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

This should work.

OTHER TIPS

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.

You can check as follows:

  File file = new File(getExternalCacheDirectory(), "mytextfile.txt" );
  if (file.exists()) {
      //Do action
   }
File logFile = new File(
        android.os.Environment.getExternalStorageDirectory()
                + "/book1/", "page2.tml");
if (logFile.exists())
    System.out.println("file exists");
else
    System.out.println("file does not exist

Do something like this :

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");

if(yourFile.exists())
{

}
String filepath = getFilesDir().getAbsolutePath(); 
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName); 
if (FileMain.exists()){ 
do somthing here                     
}else{}
File file = new File(path+filename);
if (file.exists())
{
//Do something
}

checked, this will work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top