add a null check at getting an sd card file output stream

This commit is contained in:
tibbi 2017-09-03 10:31:36 +02:00
parent ce63adf6f1
commit 9714d8cf32
2 changed files with 18 additions and 14 deletions

View File

@ -273,7 +273,9 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
} else {
val ins = zipFile.getInputStream(entry)
ins.use {
ins.copyTo(getFileOutputStream(file.absolutePath, file.getMimeType()))
val fos = getFileOutputStream(file.absolutePath, file.getMimeType())
if (fos != null)
ins.copyTo(fos)
}
}
}
@ -296,9 +298,10 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {
val queue = LinkedList<File>()
val out = getFileOutputStream(targetPath, "application/zip")
val zout = ZipOutputStream(out)
var res: Closeable = out
val fos = getFileOutputStream(targetPath, "application/zip") ?: return false
val zout = ZipOutputStream(fos)
var res: Closeable = fos
try {
sourcePaths.forEach {
@ -344,14 +347,15 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return true
}
private fun getFileOutputStream(targetPath: String, mimeType: String): OutputStream {
private fun getFileOutputStream(targetPath: String, mimeType: String): OutputStream? {
val targetFile = File(targetPath)
return if (activity.needsStupidWritePermissions(targetPath)) {
val documentFile = activity.getFileDocument(targetFile.parent)
if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_file), targetFile.parent)
throw IOException(error)
activity.showErrorToast(error)
return null
}
val newDocument = documentFile.createFile(mimeType, File(targetPath).name)

View File

@ -14,7 +14,6 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
private val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
init {
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
@ -53,8 +52,8 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
}
private fun createDirectory(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
if (activity.needsStupidWritePermissions(path)) {
activity.handleSAFDialog(file) {
when {
activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) {
callback(false)
@ -63,11 +62,12 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
documentFile.createDirectory(file.name)
success(alertDialog)
}
} else if (file.mkdirs()) {
success(alertDialog)
callback(true)
} else
callback(false)
file.mkdirs() -> {
success(alertDialog)
callback(true)
}
else -> callback(false)
}
}
private fun errorOccurred() {