Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAINTROID-693 File not found when loading temporary copies #1322

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -596,18 +596,17 @@ object FileIO {

val stream = FileOutputStream("$tempPath/$newFileName")
commandSerializer.writeToInternalMemory(stream)
temporaryFilePath = TEMP_IMAGE_TEMP_PATH
stream.close()
} catch (e: IOException) {
Log.e("Cannot write", "Can't write to stream", e)
}
val oldFile = File(internalMemoryPath, TEMP_IMAGE_PATH)
if (oldFile.exists()) {
val newFile = File(internalMemoryPath, TEMP_IMAGE_TEMP_PATH)
if (oldFile.exists() && newFile.exists()) {
oldFile.delete()
}
val newFile = File(internalMemoryPath, TEMP_IMAGE_TEMP_PATH)
if (newFile.exists()) {
newFile.renameTo(File(internalMemoryPath, TEMP_IMAGE_PATH))
temporaryFilePath = TEMP_IMAGE_PATH
}
}

Expand All @@ -616,34 +615,22 @@ object FileIO {
if (!tempPath.exists()) {
return false
}
val fileList = tempPath.listFiles()
if (fileList != null && fileList.isNotEmpty()) {
if (fileList.size == 2) {
if (fileList[1].lastModified() > fileList[0].lastModified()) {
reorganizeTempFiles(fileList[1], fileList[0], internalMemoryPath)
} else {
reorganizeTempFiles(fileList[0], fileList[1], internalMemoryPath)
}
} else {
temporaryFilePath = fileList[0].path
}
val file = File(internalMemoryPath, TEMP_IMAGE_PATH)
if (file.exists()) {
temporaryFilePath = file.path
return true
}
return false
}

private fun reorganizeTempFiles(file1: File, file2: File, internalMemoryPath: File) {
file2.delete()
file1.renameTo(File(internalMemoryPath, TEMP_IMAGE_PATH))
temporaryFilePath = TEMP_IMAGE_PATH
}

fun openTemporaryPictureFile(commandSerializer: CommandSerializer): WorkspaceReturnValue? {
var workspaceReturnValue: WorkspaceReturnValue? = null
if (temporaryFilePath != null) {
try {
val stream = FileInputStream(temporaryFilePath)
if (stream.available() == 0) throw IOException("FileInputStream not available!")
workspaceReturnValue = commandSerializer.readFromInternalMemory(stream)
stream.close()
} catch (e: IOException) {
Log.e("Cannot read", "Can't read from stream", e)
}
Expand Down