-1

I think this question may be a duplicate but I didn't find any other answers for this.

My question is:

Is it possible to know whether a file (e.g.: a .txt/.doc/.csv file) exists or not in any location (i.e. internal/external storage) in the device?

user2340612
  • 9,145
  • 4
  • 40
  • 63
syam vakkalanka
  • 416
  • 3
  • 14

2 Answers2

0

If you already know the path of the file you can use the following code:

File yourFile = new File(yourFilePath);
yourFile.exists();
user2340612
  • 9,145
  • 4
  • 40
  • 63
Niza Siwale
  • 2,356
  • 1
  • 18
  • 20
0

Hope it works in your case.

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "filename with extention");
File file2 = new File("internal sotrage path here" + File.separator + "filename with extention");
try {
    if(file.exists()){
        // code if file exist in external storage
    }
    else if(file2.exists()){
        // code if file exist in internal storage
    }
    else {
        // file not found
    }
} catch (IOException e) {
    e.printStackTrace();
}
Pang
  • 9,073
  • 146
  • 84
  • 117
Khizar Hayat
  • 3,049
  • 2
  • 16
  • 22