Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists on SD card on Android

Tags:

android

I want to check if a text file exists on the SD card. The file name is mytextfile.txt. Below is the code:

FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);

How can I check whether this file exists?

like image 278
androiddevs78 Avatar asked Oct 08 '11 15:10

androiddevs78


3 Answers

This should do the trick, I've replaced the hard coded SDcard reference to the recommended API call getExternalCacheDir():

File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
  //Do action
}
like image 55
Ljdawson Avatar answered Sep 23 '22 21:09

Ljdawson


See this file System in android : Working with SDCard’s filesystem in Android

you just check

if(file.exists()){
//
}
like image 36
Samir Mangroliya Avatar answered Sep 22 '22 21:09

Samir Mangroliya


*Using this you can check the file is present or not in sdcard *

File file = new File(sdcardpath+ "/" + filename);
if (file.exists())
 {

}
like image 29
Neelaganda Moorthy Avatar answered Sep 23 '22 21:09

Neelaganda Moorthy