New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-13642] Android: Fixed bug where "relative" image paths won't load #10304
Conversation
- Relative image path handling was broken due to extra slash added to path, which Android asset manager could not handle. * Solved by trimming extra slashes. * Trimming not needed for filesystem paths. Was not an issue for Ti.Filesystem.getFile() API. - Note that relative paths worked for Ti.Filesystem.getFile(), but not for image loading. Android required a leading slash. - Fixed resolution of paths containing "../" when loading an image from a JS file in a subdirectory. - Added support for "content://" image URLs. (Used to only work via blobs.) - Added support for "android.resource://" image URLs. (Used to only work via blobs.)
* Returns the same string reference if no characters were trimmed. | ||
* Returns null if given a null string. | ||
*/ | ||
private static String trimFront(String sourceString, char trimCharacter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to simplify this a little:
private static String trimFront(String sourceString, char trimCharacter)
{
if (sourceString != null) {
for (int i = 0; i < sourceString.length(); i++) {
if (sourceString.charAt(i) != trimCharacter) {
sourceString = sourceString.substring(i);
break;
}
}
}
return sourceString;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't handle the case where all characters in the string are trimmable. That's why I do the substring handling outside of the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that a for
is a little easier to read than a while
in this case:
private static String trimFront(String sourceString, char trimCharacter)
{
if (sourceString != null) {
int i = 0;
for (; i < sourceString.length(); i++) {
if (sourceString.charAt(i) != trimCharacter) {
sourceString = sourceString.substring(i);
break;
}
}
if (i == sourceString.length()) {
sourceString = "";
}
}
return sourceString;
}
But they both work, so I'll let you decide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally wrote it with a for
like this...
for (; (index < sourceString.length()) && (sourceString.charAt(index) == trimCharacter); index++);
But our lint step didn't like it. (shrugs)
// Note: A "file:///" URL needs 3 slashes to reference localhost. | ||
if (defaultScheme != null) { | ||
combined = defaultScheme + PATH_SEPARATOR; | ||
if (defaultScheme.equals("file:")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use TiFileFactory.FILE_URL_SCHEME
here? Or even FILE_URL_PREFIX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we have several "file:" string literals in the "TiURL.java" code.
I also don't like how "TiURL.java" expects a scheme string to end with a trailing colon ':' either. That's not part of the scheme. That's the character which separates the scheme from the path.
I didn't feel like mucking with this part and left it alone.
FR Passed.
Studio Ver: 5.1.1.201809051655 |
Waiting for CR to pass. |
Generated by 🚫 dangerJS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CR: PASS
JIRA:
https://jira.appcelerator.org/browse/TIMOB-13642
Summary:
"../"
when loading an image from a JS file in a subdirectory.Ti.Filesystem.getResRawDirectory() + "MyImage"
to work."android.resource://" + Ti.App.id + "/drawable/MyImage"
to work.Note:
Relative paths are currently handled differently between platforms.
I have not changed this behavior since that would be a breaking-change.
Test: