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 {
// open database first
if (!_databaseManager.isOpen()) {
_databaseManager.open();
}
File tempDir = new File(activity.getCacheDir() + File.separator + UUID.randomUUID().toString());
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()) {
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();");
}

View file

@ -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<String> 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;
}

View file

@ -193,16 +193,10 @@ public class ScratchJrUtil {
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<>();
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) {