2012-07-02 106 views
7

Quiero verificar si un archivo dado existe en la tarjeta sd de Android. Lo estoy probando creando un archivo usando la ruta absoluta y comprobando con file.exists() pero no está funcionando. La URL del archivo es "file:///mnt/sdcard/book1/page2.html" y el archivo existe. Pero de alguna manera file.exists() no muestra lo mismo.Cómo verificar si un archivo existe en un directorio en la tarjeta SD

+0

duplicado posible de [Comprobar si existe el archivo en la tarjeta SD en Android] (https://stackoverflow.com/questions/7697650/check-if-file -exists-on-sd-card-on-android) –

Respuesta

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

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

esto debería funcionar.

+0

¡Muchas gracias! ¡¡Esto funciona!! – working

+0

¡Bienvenido! Por favor, márquelo aceptado, si le solucionó su problema, gracias. –

0
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 
1

Puede comprobar la siguiente manera:

File file = new File(getExternalCacheDirectory(), "mytextfile.txt"); 
    if (file.exists()) { 
     //Do action 
    } 
0

hacer algo como esto:

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

if(yourFile.exists()) 
{ 

} 
7

Trate de esta manera:

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

También asegúrese de que tiene:

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

en el archivo de manifiesto.

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

marcada, esto funcionará

Cuestiones relacionadas