Android: fix newIntent content import

This commit is contained in:
Yueyu 2021-04-09 21:39:38 +08:00
parent c186305777
commit b52045f221
3 changed files with 17 additions and 15 deletions

View file

@ -278,12 +278,17 @@ public class IOManager {
} }
public void receiveProject(ScratchJrActivity activity, Uri uri) throws JSONException, IOException, DatabaseException { 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()); File tempDir = new File(activity.getCacheDir() + File.separator + UUID.randomUUID().toString());
tempDir.mkdir(); tempDir.mkdir();
List<String> entries = ScratchJrUtil.unzip(uri.getPath(), tempDir.getPath()); List<String> entries = ScratchJrUtil.unzip(activity.getContentResolver().openInputStream(uri), tempDir.getPath());
if (entries.isEmpty()) { if (entries.isEmpty()) {
Log.e(LOG_TAG, "no entries found"); Log.e(LOG_TAG, "no entries found");
// no files // no files
ScratchJrUtil.removeFile(tempDir);
return; return;
} }
// read project json // read project json
@ -367,6 +372,8 @@ public class IOManager {
} }
_databaseManager.insert(table, asset); _databaseManager.insert(table, asset);
} }
// clean up
ScratchJrUtil.removeFile(tempDir);
// refresh lobby // refresh lobby
activity.runJavaScript("Lobby.refresh();"); activity.runJavaScript("Lobby.refresh();");
} }

View file

@ -579,11 +579,12 @@ public class JavaScriptDirectInterface {
// clean up old zip files // clean up old zip files
_activity.getIOManager().cleanZips(); _activity.getIOManager().cleanZips();
// create a temp folder // create a temp folder
File tempPath = new File(_activity.getCacheDir() + File.separator + UUID.randomUUID().toString()); File tempFolder = new File(_activity.getCacheDir() + File.separator + UUID.randomUUID().toString());
tempPath.mkdir(); File projectFolder = new File(tempFolder.getPath() + File.separator + "project");
projectFolder.mkdirs();
// save data.json // save data.json
// Log.d(LOG_TAG, "writing 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 { try {
FileOutputStream outputStream = new FileOutputStream(dataFile); FileOutputStream outputStream = new FileOutputStream(dataFile);
outputStream.write(projectData.getBytes()); outputStream.write(projectData.getBytes());
@ -605,7 +606,7 @@ public class JavaScriptDirectInterface {
Iterator<String> keys = metadata.keys(); Iterator<String> keys = metadata.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
String key = keys.next(); 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()) { if (!folder.exists()) {
folder.mkdir(); folder.mkdir();
} }
@ -636,11 +637,11 @@ public class JavaScriptDirectInterface {
String extension = ScratchJrUtil.getExtension(); String extension = ScratchJrUtil.getExtension();
String fullName = name + extension; String fullName = name + extension;
ScratchJrUtil.zipFileAtPath( ScratchJrUtil.zipFileAtPath(
tempPath.getAbsolutePath(), projectFolder.getAbsolutePath(),
_activity.getCacheDir() + File.separator + fullName _activity.getCacheDir() + File.separator + fullName
); );
// remove the temp folder // remove the temp folder
ScratchJrUtil.removeFile(tempPath); ScratchJrUtil.removeFile(tempFolder);
return fullName; return fullName;
} }

View file

@ -193,16 +193,10 @@ public class ScratchJrUtil {
return segments[segments.length - 1]; return segments[segments.length - 1];
} }
public static List<String> unzip(String zipPath, String toPath) { public static List<String> unzip(InputStream inputStream, String toPath) {
List<String> entries = new ArrayList<>(); List<String> entries = new ArrayList<>();
File zipFile = new File(zipPath);
ZipInputStream zin; ZipInputStream zin;
try { zin = new ZipInputStream(inputStream);
zin = new ZipInputStream(new FileInputStream(zipFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return entries;
}
try { try {
ZipEntry ze; ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) { while ((ze = zin.getNextEntry()) != null) {