2012-05-14 5 views
25

다음 코드는 내부 저장소에 파일이 있는지 식별하는 방법입니다 (MODE_PRIVATE).내부 저장소에 파일이 있는지 확인하려고 시도합니다.

public boolean isset(String filename){ 
    FileInputStream fos = null; 
    try { 
     fos = openFileInput(filename); 
     //fos = openFileInput(getFilesDir()+"/"+filename); 
     if (fos != null) { 
     return true; 
     }else{ 
     return false; 
     } 
    } catch (FileNotFoundException e) { 
     return false; 
    } 

    //File file=new File(mContext.getFilesDir(),filename); 

    //boolean exists = fos.exists(); 
    } 

그러나 예외가 발생하며 코드는 계속 진행되지 않습니다. 그것은 돌아 오지 않습니다. 왜?

+1

당신이 스택 추적을 우리에게 제공 할 수 있습니까? – Nicholas

답변

101

이 방법이 도움이되기를 바랍니다. 내부 저장 장치에 대한

public boolean fileExist(String fname){ 
    File file = getBaseContext().getFileStreamPath(fname); 
    return file.exists(); 
} 
+3

정답 인 Michael Petrotta 당신은 이것을 받아 들여야합니다 – Amanni

+1

@bsara 왜 응용 프로그램의 컨텍스트가 아닌 기본 컨텍스트입니까? 아니면 둘 다 작동합니까? –

+0

getApplicationContext()도 작동합니다. –

4

이 나를 위해 작동합니다

public boolean isFilePresent(String fileName) { 
    String path = getContext().getFilesDir().getAbsolutePath() + "/" + fileName; 
    File file = new File(path); 
    return file.exists(); 
} 
관련 문제