android - Extract Images from JSON Object -
i fetching popular movies api, , trying poster images each movie. need extract poster_path parameter json string.
this code extract information json string:
final string owm_results = "results"; final string owm_posterpath = "poster_path"; string aux; // moviejsonstr string full http request jsonobject moviejson = new jsonobject(moviejsonstr); jsonarray moviearray = moviejson.getjsonarray(owm_results); for(int = 0; < moviearray.length(); i++) { jsonobject movieobj = moviearray.getjsonobject(i); aux = movieobj.getstring(owm_posterpath); log.v("aux: ", aux); }
the json string looks this:
{ "page":1, "results":[ { "adult":false, "backdrop_path":"/tbhdm8ujab4victsulyfl3lxmcd.jpg", "genre_ids":[ 53, 28, 12 ], "id":76341, "original_language":"en", "original_title":"mad max: fury road", "overview":"an apocalyptic story set in furthest reaches of our planet, in stark desert landscape humanity broken, , crazed fighting necessities of life. within world exist 2 rebels on run might able restore order. there's max, man of action , man of few words, seeks peace of mind following loss of wife , child in aftermath of chaos. , furiosa, woman of action , woman believes path survival may achieved if can make across desert childhood homeland.", "release_date":"2015-05-15", "poster_path":"/kqjl17yufvn9ovlyxypvtyrffak.jpg", "popularity":46.603256, "title":"mad max: fury road", "video":false, "vote_average":7.6, "vote_count":2296 }] }
assuming jsonobject indeed have images, how can extract them , convert them can manipulate instance of drawable?
there (in general) 2 ways pass images through json. first , in opinion not best 1 put raw image data json object. can encode image base64, , put string object, decode base64 , decode raw data bitmapfactory
class.
the second , better way passing image url , seems json presented using method:
"poster_path":"/kqjl17yufvn9ovlyxypvtyrffak.jpg"
the raw solution problem presented akhil batlawala's answer. real world applications should image library picasso usage like:
picasso picasso = new picasso(); picasso.load(url).fit().into(myimageview);
Comments
Post a Comment