Tag Archives: Android

Loading images into Android Studio

Took me a good one hour to eventually figure out the problem with using filenames that do not conform to the format as expected by Android Studio. Basically I just wanted to add some pictures for display in my Android app. I have done it before, just dragging a photo, or copy and paste a photo, to drawable folder as a drawable resource. I mean I managed to do that, and the files showed up in the drawable folder. But the problem was I could not use in my code. I failed to call the photos by the id. Went round in loops, thinking that I might need to go down the path of adding image asset, rather than the way I have used before. But it did not work either, and I knew it was not the right thing to do. I wasn’t trying to turn my photo into icons or something!

Turns out that if I want to add a photo (or image or whatever you want to call it) as drawable resource, the filename of the photo cannot consist of capital letters, and no hyphen. I’m sure there must be some sense such naming restriction. But it was not well documented!!

Picasso and null path to image

Found something very odd when programming my Android mobile app today. I am aware that I should not provide a null path for Picasso to load. Hence defensive statements in my code to make sure that Picasso would not get to load a null path. But somehow my app was still crashing, with the following error:

    java.lang.IllegalArgumentException: Path must not be empty

And the error was pointing to the line where the Picasso call was located:

Picasso.get().load(“null image path”);

Even when this line would not be run, which I have checked, it was still giving me problem. Turns out that my defensive statement allowed non-null image path to happen first, as below:

if (imgPath != null || !(imgPath.isEmpty()) {
    Picasso.get().load(imgPath).into(imageView);
} else { 
    Picasso.get().load(placeholder).into(imageView);
}

If I swap the defensive statement around, such that imgPath == null is checked first, then my app will run okay. Very odd. Anyone kind enough to explain to me what was that about?