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 { } else {
val ins = zipFile.getInputStream(entry) val ins = zipFile.getInputStream(entry)
ins.use { 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 { fun zipPaths(sourcePaths: List<String>, targetPath: String): Boolean {
val queue = LinkedList<File>() val queue = LinkedList<File>()
val out = getFileOutputStream(targetPath, "application/zip") val fos = getFileOutputStream(targetPath, "application/zip") ?: return false
val zout = ZipOutputStream(out)
var res: Closeable = out val zout = ZipOutputStream(fos)
var res: Closeable = fos
try { try {
sourcePaths.forEach { sourcePaths.forEach {
@ -344,14 +347,15 @@ class ItemsAdapter(val activity: SimpleActivity, var mItems: MutableList<FileDir
return true return true
} }
private fun getFileOutputStream(targetPath: String, mimeType: String): OutputStream { private fun getFileOutputStream(targetPath: String, mimeType: String): OutputStream? {
val targetFile = File(targetPath) val targetFile = File(targetPath)
return if (activity.needsStupidWritePermissions(targetPath)) { return if (activity.needsStupidWritePermissions(targetPath)) {
val documentFile = activity.getFileDocument(targetFile.parent) val documentFile = activity.getFileDocument(targetFile.parent)
if (documentFile == null) { if (documentFile == null) {
val error = String.format(activity.getString(R.string.could_not_create_file), targetFile.parent) 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) 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) private val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
init { init {
AlertDialog.Builder(activity) AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, 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) { private fun createDirectory(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
if (activity.needsStupidWritePermissions(path)) { when {
activity.handleSAFDialog(file) { activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(file) {
val documentFile = activity.getFileDocument(file.absolutePath) val documentFile = activity.getFileDocument(file.absolutePath)
if (documentFile == null) { if (documentFile == null) {
callback(false) callback(false)
@ -63,11 +62,12 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
documentFile.createDirectory(file.name) documentFile.createDirectory(file.name)
success(alertDialog) success(alertDialog)
} }
} else if (file.mkdirs()) { file.mkdirs() -> {
success(alertDialog) success(alertDialog)
callback(true) callback(true)
} else }
callback(false) else -> callback(false)
}
} }
private fun errorOccurred() { private fun errorOccurred() {