Created
August 8, 2019 10:32
-
-
Save arindamxd/e56c4b1fac626c7bc6ce54c6c5a3d57a to your computer and use it in GitHub Desktop.
Write Json to File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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