less than 1 minute read

private String writeToFile(String data,String filename) {
if(isStoragePermissionGranted()){
Toast.makeText(context,”permission deny”,Toast.LENGTH_LONG);
}
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS+”/”+context.getPackageName());
// File dir = context.getFilesDir();

    if(!dir.exists()){  
        dir.mkdirs();  
    }  
    File file = new File(dir, filename);  
    OutputStream out = null;  
    try {  
        file.createNewFile();  
        file.setReadable(true, false);  
        file.setExecutable(true, false);  
        file.setWritable(true, false);  
        out = new FileOutputStream(file);  
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);   //            File file = new File(context.getFilesDir(), filename);   //            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE));  
        outputStreamWriter.write(data);  
        outputStreamWriter.flush();  
        outputStreamWriter.close();  
        Log.v("@writeToFile","[Success] "+file.getAbsolutePath());  
    }  
    catch (IOException e) {  
        e.printStackTrace();  
        Log.e("Exception", "File write failed: " + e.toString());  
    }  
    MediaScannerConnection.scanFile(context, new String[] {file.toString()}, null, null); // <-- 이부분 추가된 파일을 이름으로 스캔을 다시 하도록 함   //        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory())));  
    return file.getAbsolutePath();  
}

🔗original-link

Updated: