2012-07-19 1 views
0

내 응용 프로그램은 SD 카드의 일부 이미지를 만들어 사용합니다. 이 이미지는 장치 갤러리에 표시되지만 원하는 것은 아닙니다. 그래서이 디렉토리에 .nonmedia 파일을 만들려고했지만 내 문제는이 파일을 만들 수 없다는 것입니다.안드로이드에서 .nonmedia 파일 만들기 does not work

을 Heres 코드 :

public void createNonmediaFile(){ 
    String text = "NONEMEDIA"; 
    String path = Environment.getExternalStorageDirectory().getPath() + "/" + AVATARS + "/.nonmedia"; 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(path); 
     fos.write(text.getBytes()); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

는 예외는 없습니다.

나는 그것이 "." 이름으로. 동일한 whithout 시도하면 파일이 만들어집니다.

도움 주셔서 감사합니다.

+1

파일을 '.nomedia '라고해야합니다. 자신이 가지고있는 것과 철자법의 차이점에 유의하십시오. – wsanville

답변

1

File file = new File(directoryPath, ".nomedia"); 
    if (!file.exists()) { 
     try { 
      file.createNewFile(); 
     } 
     catch(IOException e) { 

     } 
    } 
0

은 안드로이드 매니페스트 파일에 권한을 아래에 넣어 다음 예 사용해보십시오 : 잘 작동한다

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

그리고 코드 아래를 :

private static final String AVATARS = "avatars"; 
public void createNonmediaFile(){ 
    String text = "NONEMEDIA"; 
    String path = Environment.getExternalStorageDirectory().getPath() + "/" + AVATARS + "/.nonmedia"; 
    String f = Environment.getExternalStorageDirectory().getPath() + "/" + AVATARS ; 
    FileOutputStream fos; 
    try { 
     File folder = new File(f); 
     boolean success=false; 
     if (!folder.exists()) { 
      success = folder.mkdir(); 
     } 
     if (true==success) { 
      File yourFile = new File(path); 
      if(!yourFile.exists()) { 
       yourFile.createNewFile(); 
      } 
     } else { 
     // Do something else on failure 
     } 
     fos = new FileOutputStream(path); 
     fos.write(text.getBytes()); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
}