Skip to content

Instantly share code, notes, and snippets.

@arindamxd
Created August 8, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamxd/e56c4b1fac626c7bc6ce54c6c5a3d57a to your computer and use it in GitHub Desktop.
Save arindamxd/e56c4b1fac626c7bc6ce54c6c5a3d57a to your computer and use it in GitHub Desktop.
Write Json to File
/**
* Writes Json/Gson to a temporary file and returns the Uri for the File
*
* @param context Application Context
* @param any File Content
* @param fileName Json File Name
*
* @return Uri for temp file with json
* @throws FileNotFoundException Throws if json file cannot be found
*/
@Throws(FileNotFoundException::class)
internal fun writeJsonToFile(context: Context, jsonString: String, directoryName: String, fileName: String): Uri {
val outputDir = File(context.filesDir, "$OUTPUT_PATH/$directoryName")
if (!outputDir.exists()) {
outputDir.mkdirs() // should succeed
}
val outputFile = File(outputDir, fileName)
try {
FileWriter(outputFile).use { it.write(jsonString) }
} catch (e: Exception) {
logStackTrace(e)
}
return Uri.fromFile(outputFile)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment