refactor(Utils): save to downloads better
This commit is contained in:
parent
f8e99bad89
commit
eefb7108dc
1 changed files with 8 additions and 7 deletions
|
|
@ -28,18 +28,19 @@ fun Context.copyToClipboard(text: String) {
|
|||
)
|
||||
}
|
||||
|
||||
fun Context.saveFile(name: String, text: String): File? {
|
||||
val basePath = Environment.getExternalStorageDirectory()
|
||||
val file = File(basePath, "/Download/$name")
|
||||
fun Context.saveFile(name: String, text: String): Boolean {
|
||||
val downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
||||
val file = File(downloads, name)
|
||||
val base = downloads.parentFile!!
|
||||
|
||||
return try {
|
||||
file.writeText(text)
|
||||
showToast(R.string.installer_file_save_success, file.relativeTo(basePath))
|
||||
file
|
||||
showToast(R.string.installer_file_save_success, file.toRelativeString(base))
|
||||
true
|
||||
} catch (e: Throwable) {
|
||||
Log.e(BuildConfig.TAG, Log.getStackTraceString(e))
|
||||
showToast(R.string.installer_file_save_failed, file.relativeTo(basePath))
|
||||
null
|
||||
showToast(R.string.installer_file_save_failed, file.toRelativeString(base))
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue