Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. /**
  2. * Writes Json/Gson to a temporary file and returns the Uri for the File
  3. *
  4. * @param context Application Context
  5. * @param any File Content
  6. * @param fileName Json File Name
  7. *
  8. * @return Uri for temp file with json
  9. * @throws FileNotFoundException Throws if json file cannot be found
  10. */
  11. @Throws(FileNotFoundException::class)
  12. internal fun writeJsonToFile(context: Context, jsonString: String, directoryName: String, fileName: String): Uri {
  13.  
  14. val outputDir = File(context.filesDir, "$OUTPUT_PATH/$directoryName")
  15. if (!outputDir.exists()) {
  16. outputDir.mkdirs() // should succeed
  17. }
  18.  
  19. val outputFile = File(outputDir, fileName)
  20. try {
  21. FileWriter(outputFile).use { it.write(jsonString) }
  22. } catch (e: Exception) {
  23. logStackTrace(e)
  24. }
  25.  
  26. return Uri.fromFile(outputFile)
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement