diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/IOManager.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/IOManager.java index cf29e15..f9ec5af 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/IOManager.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/IOManager.java @@ -278,12 +278,17 @@ public class IOManager { } public void receiveProject(ScratchJrActivity activity, Uri uri) throws JSONException, IOException, DatabaseException { + // open database first + if (!_databaseManager.isOpen()) { + _databaseManager.open(); + } File tempDir = new File(activity.getCacheDir() + File.separator + UUID.randomUUID().toString()); tempDir.mkdir(); - List entries = ScratchJrUtil.unzip(uri.getPath(), tempDir.getPath()); + List entries = ScratchJrUtil.unzip(activity.getContentResolver().openInputStream(uri), tempDir.getPath()); if (entries.isEmpty()) { Log.e(LOG_TAG, "no entries found"); // no files + ScratchJrUtil.removeFile(tempDir); return; } // read project json @@ -367,6 +372,8 @@ public class IOManager { } _databaseManager.insert(table, asset); } + // clean up + ScratchJrUtil.removeFile(tempDir); // refresh lobby activity.runJavaScript("Lobby.refresh();"); } diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java index c432d21..32479f7 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/JavaScriptDirectInterface.java @@ -579,11 +579,12 @@ public class JavaScriptDirectInterface { // clean up old zip files _activity.getIOManager().cleanZips(); // create a temp folder - File tempPath = new File(_activity.getCacheDir() + File.separator + UUID.randomUUID().toString()); - tempPath.mkdir(); + File tempFolder = new File(_activity.getCacheDir() + File.separator + UUID.randomUUID().toString()); + File projectFolder = new File(tempFolder.getPath() + File.separator + "project"); + projectFolder.mkdirs(); // save data.json // Log.d(LOG_TAG, "writing data.json"); - File dataFile = new File(tempPath.getAbsolutePath() + File.separator + "data.json"); + File dataFile = new File(projectFolder.getAbsolutePath() + File.separator + "data.json"); try { FileOutputStream outputStream = new FileOutputStream(dataFile); outputStream.write(projectData.getBytes()); @@ -605,7 +606,7 @@ public class JavaScriptDirectInterface { Iterator keys = metadata.keys(); while (keys.hasNext()) { String key = keys.next(); - File folder = new File(tempPath.getAbsolutePath() + File.separator + key); + File folder = new File(projectFolder.getAbsolutePath() + File.separator + key); if (!folder.exists()) { folder.mkdir(); } @@ -636,11 +637,11 @@ public class JavaScriptDirectInterface { String extension = ScratchJrUtil.getExtension(); String fullName = name + extension; ScratchJrUtil.zipFileAtPath( - tempPath.getAbsolutePath(), + projectFolder.getAbsolutePath(), _activity.getCacheDir() + File.separator + fullName ); // remove the temp folder - ScratchJrUtil.removeFile(tempPath); + ScratchJrUtil.removeFile(tempFolder); return fullName; } diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrUtil.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrUtil.java index ad5e96f..c967ff7 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrUtil.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrUtil.java @@ -193,16 +193,10 @@ public class ScratchJrUtil { return segments[segments.length - 1]; } - public static List unzip(String zipPath, String toPath) { + public static List unzip(InputStream inputStream, String toPath) { List entries = new ArrayList<>(); - File zipFile = new File(zipPath); ZipInputStream zin; - try { - zin = new ZipInputStream(new FileInputStream(zipFile)); - } catch (FileNotFoundException e) { - e.printStackTrace(); - return entries; - } + zin = new ZipInputStream(inputStream); try { ZipEntry ze; while ((ze = zin.getNextEntry()) != null) {