mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00
fix(android): StringIndexOutOfBoundsException with invalid drawables (#9563)
* fix(android/application): org.nativescript.widgets.Utils::getDrawable * chore: fix spacing * fix(android/application): do not load empty path Co-authored-by: Igor Randjelovic <rigor789@gmail.com> * test: Add tests for empty image sources * chore: add a few more test cases These make the app crash without the fix in place Co-authored-by: Igor Randjelovic <rigor789@gmail.com> Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
<Page>
|
||||
<GridLayout rows="*,*,*,*,*,*" >
|
||||
<GridLayout rows="*,*,*,*,*,*,*,*,*,*,*,*,*" >
|
||||
<Button backgroundImage="url('~/resources/images/no-image.png')"
|
||||
borderRadius='125' borderWidth='2' borderColor='black'
|
||||
backgroundRepeat="repeat" backgroundSize="contain"
|
||||
@ -25,5 +25,35 @@
|
||||
backgroundSize="contain"
|
||||
height="80" width="180"
|
||||
/>
|
||||
|
||||
<!-- Test some invalid cases - these should not crash -->
|
||||
<Button row="6" backgroundImage="url('res://theneverfoundunicorn.png')"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="7" backgroundImage="url('res://')"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="8" backgroundImage="url('')"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="9" backgroundImage="res://theneverfoundunicorn.png"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="10" backgroundImage="res://"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="11" backgroundImage=" "
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
<Button row="12" backgroundImage="bad"
|
||||
borderRadius='10' borderWidth='2' borderColor='black'
|
||||
height="80" width="180"
|
||||
/>
|
||||
</GridLayout>
|
||||
</Page>
|
||||
|
@ -12,6 +12,8 @@
|
||||
|
||||
<Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" />
|
||||
<Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" />
|
||||
<Image src="res://" margin="1" width="30" height="30" backgroundColor="yellow" />
|
||||
<Image src="null" margin="1" width="30" height="30" backgroundColor="yellow" />
|
||||
|
||||
<Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" />
|
||||
<Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" />
|
||||
|
@ -36,8 +36,14 @@ import java.util.concurrent.Executors;
|
||||
|
||||
public class Utils {
|
||||
public static Drawable getDrawable(String uri, Context context){
|
||||
String resPath = uri.substring("res://".length());
|
||||
int resId = context.getResources().getIdentifier(resPath, "drawable", context.getPackageName());
|
||||
int resId = 0;
|
||||
int resPrefixLength = "res://".length();
|
||||
|
||||
if (uri.length() > resPrefixLength) {
|
||||
String resPath = uri.substring(resPrefixLength);
|
||||
resId = context.getResources().getIdentifier(resPath, "drawable", context.getPackageName());
|
||||
}
|
||||
|
||||
if (resId > 0) {
|
||||
return AppCompatResources.getDrawable(context, resId);
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user