简简单单的 文字写入磁盘,f方便测试临时数据保存
object FileUtil {
fun saveBitmap(context: Context, bitmap: Bitmap?) = WorkScope.launch {
val dir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val toString = System.currentTimeMillis().toString()
if (dir != null) {
saveFile(bitmap, dir.path, "$toString.jpg")
}
}
private fun saveFile(bitmap: Bitmap?, path: String, fileName: String): File? {
bitmap ?: return null
val dirFile = File(path)
if (!dirFile.exists()) {
dirFile.mkdir()
}
val myCaptureFile = File(path, fileName)
try {
val bos = BufferedOutputStream(FileOutputStream(myCaptureFile))
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)
bos.flush()
bos.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return myCaptureFile
}
fun saveJson(json: String, context: Context) = WorkScope.launch {
val dir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val toString = System.currentTimeMillis().toString()
if (dir != null) {
saveJson(json, dir.path, "$toString.json")
}
}
fun saveJson(json: String, path: String, fileName: String): File {
val dirFile = File(path)
if (!dirFile.exists()) {
dirFile.mkdir()
}
val myCaptureFile = File(path, fileName)
try {
val bos = BufferedWriter(FileWriter(myCaptureFile))
bos.write(json)
bos.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return myCaptureFile
}
}
|