Skip to content

Commit 896a76c

Browse files
committed
dataPath() checks if name corresponds to asset first
1 parent f452ad6 commit 896a76c

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

core/src/processing/core/PApplet.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5572,20 +5572,31 @@ public File saveFile(String where) {
55725572
/**
55735573
* Return a full path to an item in the data folder.
55745574
* <p>
5575-
* In this method, the data path is defined not as the applet's actual
5576-
* data path, but a folder titled "data" in the sketch's working
5577-
* directory. When running inside the PDE, this will be the sketch's
5578-
* "data" folder. However, when exported (as application or applet),
5579-
* sketch's data folder is exported as part of the applications jar file,
5580-
* and it's not possible to read/write from the jar file in a generic way.
5581-
* If you need to read data from the jar file, you should use createInput().
5575+
* The behavior of this function differs from the equivalent on the Java mode: files stored in
5576+
* the data folder of the sketch get packed as assets in the apk, and the path to the data folder
5577+
* is no longer valid. Only the name is needed to open them. However, if the file is not an asset,
5578+
* we can assume it has been created by the sketch, so it should have the sketch path.
5579+
* Discussed here:
5580+
* https://github.com/processing/processing-android/issues/450
55825581
*/
55835582
public String dataPath(String where) {
5584-
// isAbsolute() could throw an access exception, but so will writing
5585-
// to the local disk using the sketch path, so this is safe here.
5586-
if (new File(where).isAbsolute()) return where;
5587-
5588-
return sketchPath + File.separator + "data" + File.separator + where;
5583+
// First, we check if it is asset:
5584+
boolean isAsset = false;
5585+
AssetManager assets = surface.getAssets();
5586+
InputStream is = null;
5587+
try {
5588+
is = assets.open(where);
5589+
isAsset = true;
5590+
} catch (IOException ex) {
5591+
//file does not exist
5592+
} finally {
5593+
try {
5594+
is.close();
5595+
} catch (Exception ex) { }
5596+
}
5597+
if (isAsset) return where;
5598+
// Not an asset, let's just use sketch path:
5599+
return sketchPath(where);
55895600
}
55905601

55915602

0 commit comments

Comments
 (0)