|
@@ -19,6 +19,10 @@ import java.io.*
|
|
|
|
|
|
object FileUtils {
|
|
|
|
|
|
+ fun getProjectPath(project: Project): String {
|
|
|
+ return File(Config.PROJECTS_FOLDER, project.name).absolutePath
|
|
|
+ }
|
|
|
+
|
|
|
fun getVideoFile(project: Project): File {
|
|
|
val projectFolder = File(Config.PROJECTS_FOLDER, project.name)
|
|
|
return File(projectFolder, Config.VIDEO_FILE_NAME)
|
|
@@ -150,10 +154,10 @@ object FileUtils {
|
|
|
return "com.google.android.apps.photos.content" == uri.authority
|
|
|
}
|
|
|
|
|
|
- fun saveGifForActor(context: Context, uri: Uri, fileName: String) {
|
|
|
- val dir = File(Config.IMAGE_FOLDER,"")
|
|
|
- dir.mkdir()
|
|
|
- val file = File(Config.IMAGE_FOLDER, fileName.plus(".gif"))
|
|
|
+ fun saveGifForActor(context: Context, project: Project, uri: Uri, fileName: String) {
|
|
|
+ val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
|
|
|
+ dir.mkdirs()
|
|
|
+ val file = File(dir, fileName.plus(".gif"))
|
|
|
try {
|
|
|
var sourceFile = File(getRealPathFromURI(context, uri))
|
|
|
val bis = BufferedInputStream(FileInputStream(sourceFile))
|
|
@@ -176,10 +180,10 @@ object FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun saveImageForActor(context: Context, uri: Uri, fileName: String){
|
|
|
- val dir = File(Config.IMAGE_FOLDER,"")
|
|
|
- dir.mkdir()
|
|
|
- val file = File(Config.IMAGE_FOLDER, fileName)
|
|
|
+ fun saveImageForActor(context: Context, project: Project, uri: Uri, fileName: String){
|
|
|
+ val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
|
|
|
+ dir.mkdirs()
|
|
|
+ val file = File(dir, fileName.plus(".jpg"))
|
|
|
try {
|
|
|
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
|
|
|
val bos = BufferedOutputStream(FileOutputStream(file))
|
|
@@ -196,10 +200,10 @@ object FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun saveBitmapForActor(context: Context, bitmap: Bitmap?, fileName: String){
|
|
|
- val dir = File(Config.IMAGE_FOLDER,"")
|
|
|
- dir.mkdir()
|
|
|
- val file = File(Config.IMAGE_FOLDER, fileName.plus(".png"))
|
|
|
+ fun saveBitmapForActor(context: Context, project: Project, bitmap: Bitmap?, fileName: String){
|
|
|
+ val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
|
|
|
+ dir.mkdirs()
|
|
|
+ val file = File(dir, fileName.plus(".png"))
|
|
|
try {
|
|
|
val bos = BufferedOutputStream(FileOutputStream(file))
|
|
|
bitmap?.compress(Bitmap.CompressFormat.PNG, 80, bos)
|
|
@@ -215,10 +219,80 @@ object FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun readFileToString(file: File): String {
|
|
|
+ if (!file.exists()) return ""
|
|
|
+ return FileReader(file).run {
|
|
|
+ val text = readText()
|
|
|
+ close()
|
|
|
+ text
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
fun changeProjectName(oldName: String, newName: String): Boolean {
|
|
|
+ val oldProjectFile = File("${Config.PROJECTS_FOLDER}/$oldName/${Config.PROJECT_FILE_NAME}")
|
|
|
+ if (!oldProjectFile.exists())
|
|
|
+ return false
|
|
|
+ var oldProject = Gson().fromJson<Project>(readFileToString(oldProjectFile), Project::class.java)
|
|
|
+
|
|
|
val oldProjectFolder = File(Config.PROJECTS_FOLDER, oldName)
|
|
|
val newProjectFolder = File(Config.PROJECTS_FOLDER, newName)
|
|
|
- return oldProjectFolder.renameTo(newProjectFolder)
|
|
|
+ val result = oldProjectFolder.renameTo(newProjectFolder)
|
|
|
+
|
|
|
+ if (result) {
|
|
|
+
|
|
|
+ val newProjectFile = File("${Config.PROJECTS_FOLDER}/$newName/${Config.PROJECT_FILE_NAME}")
|
|
|
+ var oldProjectPath = File(Config.PROJECTS_FOLDER, oldName).absolutePath
|
|
|
+ var newProjectPath = File(Config.PROJECTS_FOLDER, newName).absolutePath
|
|
|
+ fun changeFilePath(file: File): File {
|
|
|
+ println("resultProjectPath = ${file.absolutePath.replace(oldProjectPath, newProjectPath)}")
|
|
|
+ return File(file.absolutePath.replace(oldProjectPath, newProjectPath))
|
|
|
+ }
|
|
|
+ println("oldProjectPath = $oldProjectPath")
|
|
|
+ println("newProjectPath = $newProjectPath")
|
|
|
+ val newProject = Project(
|
|
|
+ oldProject.themeAssetIndex,
|
|
|
+ changeFilePath(oldProject.projectFile),
|
|
|
+ changeFilePath(oldProject.projectFolder),
|
|
|
+ changeFilePath(oldProject.scene1File),
|
|
|
+ changeFilePath(oldProject.scene2File),
|
|
|
+ changeFilePath(oldProject.coverFile),
|
|
|
+ changeFilePath(oldProject.assetFolder),
|
|
|
+ newName,
|
|
|
+ oldProject.author,
|
|
|
+ oldProject.frontCoverColor,
|
|
|
+ oldProject.backCoverColor,
|
|
|
+ oldProject.category,
|
|
|
+ oldProject.scene1,
|
|
|
+ oldProject.scene2,
|
|
|
+ oldProject.story,
|
|
|
+ oldProject.actorResource,
|
|
|
+ oldProject.sceneResource
|
|
|
+ )
|
|
|
+
|
|
|
+ val scenes = newProject.story?.scenes
|
|
|
+ if (scenes != null) {
|
|
|
+ var count = 0
|
|
|
+ for (scene in scenes) {
|
|
|
+ for (layer in scene.layers) {
|
|
|
+ for (actor in layer.actors) {
|
|
|
+ actor.resourcePath = actor.resourcePath.replace(oldProjectPath, newProjectPath)
|
|
|
+ count++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ println("count = $count")
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ val fileWriter = FileWriter(newProjectFile)
|
|
|
+ fileWriter.write(Gson().toJson(newProject))
|
|
|
+ fileWriter.flush()
|
|
|
+ fileWriter.close()
|
|
|
+ } catch (e: Exception) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result
|
|
|
}
|
|
|
|
|
|
fun saveProject(context: Context, project: Project, scaleWidth: Int, scaleHeight: Int): Completable {
|
|
@@ -260,6 +334,7 @@ object FileUtils {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ fileWriter.close()
|
|
|
} catch (e: Exception) {
|
|
|
e.printStackTrace()
|
|
|
}
|