0

I'm working with a tutorial fromRay Wenderlich (https://www.raywenderlich.com/124438/android-listview-tutorial). In the tutorial, an image is retrieved via the Internet using a URL specified in the locally stored JSON file:

"image" : "https://www.edamam.com/web-img/341/3417c234dadb687c0d3a45345e86bff4.jpg"

The string is stored in the imageUrl variable:

recipe.imageUrl = recipes.getJSONObject(i).getString("image");

Then the image is loaded using Picasso:

Picasso.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap
    .ic_launcher).into(thumbnailImageView);

I would like to change the code in the tutorial so that the image is retrieved from the drawable folder within the app, rather than via the Internet. I've been working on this on and off for several days. I assumed it was a matter of changing the URL in the JSON data so that it specified a path to the image file in the drawable folder like this:

"image" : "android.resource://com.raywenderlich.alltherecipes/drawable/chicken.jpg"

So far I've been unsuccessful. Am I on the right path, or am I way off? I'm an Android newbie. I'm used to working with plists in Xcode (although I'm no expert there, either).

bronyaur
  • 9
  • 3
  • Isnt there a ....loadFromResource(R.drawable.chicken).... ? – greenapps Mar 02 '18 at 21:34
  • https://stackoverflow.com/questions/21158425/picasso-load-drawable-resources-from-their-uri – greenapps Mar 02 '18 at 21:36
  • The data is being loaded from JSON into a listview so that each row has a different image name. Otherwise, the loadFromResource would work for a single use image. The link provided to a prior StackerOverflow question seems relevant. There is one reference to loading internal images dynamically but I can't determine where the referenced code might be used. Thanks for the suggestions. – bronyaur Mar 02 '18 at 23:36
  • Still not sure why a path in the JSON wouldn't work since the http: path works. It seems I should simply be able to specify a different path to obtain the image. – bronyaur Mar 02 '18 at 23:47
  • You can of course supply a different path for an image. As long as that image is on the file systsm of your Android device. But you have no such path. On your pc you put an image in the drawables folder of your project i understood. (If not please tell). That becomes a resource for your ap reachable with R.drawable.chicken. The link i provided shows you all. – greenapps Mar 03 '18 at 07:34
  • You have to change so little to make it work with resources instead of the file system. So little! Only ....load(....)... – greenapps Mar 03 '18 at 07:36
  • But adding load(R.drawable.chicken) only loads the chicken image. I need to load the image that is specified in the address obtained from the JSON data and stored in the imageUrl variable. It will be a different image depending on the row in the listview. My question is, how do you alter the tutorial code to obtain the image locally rather than from the Internet. The project specifies an http: path. I’m asking if I can substitute that path with a local path such as “app/res/drawable/image.jpg” or something similar. In the project, the path exists in the JSON data, not in java code. – bronyaur Mar 03 '18 at 14:20
  • If you want to use a local path then that is possible of course. Just put your images in a local folder and use their paths. – greenapps Mar 03 '18 at 14:36
  • If you want to load all images from resource than put them all in the drawable folder of the project on your pc. Then use R.drawable.xxxxx. – greenapps Mar 03 '18 at 14:37
  • For a local path you only have to change `load(recipe.imageUrl)` to `load(recipe.imagepath)` Or you do not change that code but care that recipe.image.Url1 contains a local path. – greenapps Mar 03 '18 at 14:39
  • `with a local path such as “app/res/drawable/image.jpg”` Wel that is not a local path on your device but a relative path on your development pc. A local path would be something like `/storage/emulated/0/myimages/chicken.png`. – greenapps Mar 03 '18 at 14:42
  • When you remove the variable and replace it with a path, you only get the image you specify in the path, not the image path stored in the variable. I need the image path stored in the variable. I need to know if I can replace the http: image path in the JSON data with a path to an image stored in the drawable folder within the app, and if so, what path should I use? I feel like I’ve tried everything. With all due respect and appreciation for your time, you’re not addressing my question. – bronyaur Mar 03 '18 at 15:04
  • You wanna change the json file you downloaded from a server? And first tell where your images reside please On a server? In a local directory? In the drawables folder on your development pc? – greenapps Mar 03 '18 at 15:17
  • So it is a local json file you said. Well than lines like `"image" : "chicken.jpg"` would do. Or `"image" : "file:///storage/emulated/0/chicken.jpg"`. But then i wonder how that json file came on your device. You did tell nothing further so what do you expect from us? – greenapps Mar 03 '18 at 15:28
  • I specified from the very beginning that I was working with a tutorial and provided the link. The JSON file is a local file. It is not being downloaded from a server. Replacing the paths that you suggested does not work. – bronyaur Mar 03 '18 at 16:20
  • So it is a local file on your Android device. If you only tell that it does not work we cannot help you. – greenapps Mar 03 '18 at 18:41
  • I've told you waaaaay more than the fact that it does not work. The very first part of my original posted question says, "In the tutorial, an image is retrieved via the Internet using a URL specified in the locally stored JSON file". If you had actually read my question it wouldn't have taken you 9 comments to find that out. Again, while I appreciate your willingness to help, you are not addressing my question because it is apparent you have not read it. – bronyaur Mar 03 '18 at 19:14

0 Answers0