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? {
|
fun Context.saveFile(name: String, text: String): Boolean {
|
||||||
val basePath = Environment.getExternalStorageDirectory()
|
val downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
||||||
val file = File(basePath, "/Download/$name")
|
val file = File(downloads, name)
|
||||||
|
val base = downloads.parentFile!!
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
file.writeText(text)
|
file.writeText(text)
|
||||||
showToast(R.string.installer_file_save_success, file.relativeTo(basePath))
|
showToast(R.string.installer_file_save_success, file.toRelativeString(base))
|
||||||
file
|
true
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Log.e(BuildConfig.TAG, Log.getStackTraceString(e))
|
Log.e(BuildConfig.TAG, Log.getStackTraceString(e))
|
||||||
showToast(R.string.installer_file_save_failed, file.relativeTo(basePath))
|
showToast(R.string.installer_file_save_failed, file.toRelativeString(base))
|
||||||
null
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue