mirror of
https://github.com/scratchfoundation/scratchjr.git
synced 2024-11-28 18:15:37 -05:00
Android: fix newIntent content import
This commit is contained in:
parent
c186305777
commit
b52045f221
3 changed files with 17 additions and 15 deletions
|
@ -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();");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue