2016-12-20 1 views
-1

다음 코드는 디렉토리를 만들려고 시도했지만이 코드는 marshmallow까지만 작동하지만 Android 'N'에는 작동하지 않습니다.Android N에 파일 만들기

File direct = new File(Environment.getExternalStorageDirectory() + "/MyApp/Drawing"); 
if (!direct.exists()) { 
    if (direct.mkdirs()) { 
     Log.v(TAG, "Directory created"); 
    } 
    else { 
     Log.v(TAG, "Directory doesn't created"); 
    } 
} 

File file = new File(direct, "newfile.jpg"); 
Uri contentUri = FileProvider.getUriForFile(HandDrawingActivity.this, "com.example.fileprovider", file); 
String fname = contentUri.getPath(); 
try { 
    FileOutputStream fos = new FileOutputStream(fname); 
} catch (FileNotFoundException e){ 
    e.printStackTrace(); 
} 

여기이 파일에는 예외가 없습니다. 참고 : 디렉터리가 만들어지고 파일이 만들어지지 않습니다. 파일이 이미 있으면 파일을 열 수 있습니다. 도움이 될 것입니다.

+5

7.0의 사용 권한 변경 사항을 확인하십시오. https://developer.android.com/about/versions/nougat/android-7.0-changes.html#perm –

+0

@ThomasRoulin FileProvider를 사용하여 기존 파일을 열 수 있습니다. 내가 그것을 사용하여 새 디렉토리를 만드는 방법을 알 수 있습니까? – Gunaseelan

+1

이 파일을 시도하십시오 direct = new File (Environment.getExternalStorageDirectory(), "Drawing"); –

답변

0

이 코드는 FileProvider을 삭제하고 다음 코드를 추가했습니다. 그리고 지금이 작품.

try { 
    if(f.createNewFile()){ 
     Log.v(TAG, "File created"); 
    }else { 
     Log.v(TAG, "File doesn't created"); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

String fname = f.getPath(); 
FileOutputStream fos; 
try { 
    fos = new FileOutputStream(fname); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

이제 파일이 만들어지고 매력적으로 작동합니다.