windows phone 8 - WP8 Some images not downloading using HttpClient -
i building wp8 app downloads images using httpclient in background task. problem images not downloaded no matter how much time wait them finish. image sizes few megabytes @ maximum.
the code utilize download images:
internal static async task<bool> download_wallpaper(string image_url, string file_name, string destination_folder_name) { seek { using (var client = new httpclient()) { // 12mb max images client.timeout = timespan.fromseconds(5); client.maxresponsecontentbuffersize = devicestatus.applicationmemoryusagelimit / 2; //client.timeout = timespan.fromseconds(5); byte[] image_byte_arr; seek { /* var requestmessage = new httprequestmessage( httpmethod.get, image_url ); var responsemessage = await client.sendasync((requestmessage)); // byte array of image image_byte_arr = await responsemessage.content.readasbytearrayasync(); */ // byte array of image image_byte_arr = await client.getbytearrayasync(image_url); } // not download grab (outofmemoryexception x) { gc.collect(); homecoming false; } var folder = await storagefolder.getfolderfrompathasync(destination_folder_name); // create file storagefile file = await folder.createfileasync(file_name, creationcollisionoption.replaceexisting); using (var write_stream = await file.openstreamforwriteasync()) { write_stream.write(image_byte_arr, 0, image_byte_arr.length); } console.writeline(devicestatus.applicationcurrentmemoryusage); homecoming true; } } grab (httprequestexception x) { console.writeline(x); homecoming false; } grab (outofmemoryexception x) { gc.collect(); homecoming false; } grab (exception x) { console.writeline(x); homecoming false; } }
this illustration image fails download: https://upload.wikimedia.org/wikipedia/commons/9/95/tracy_caldwell_dyson_in_cupola_iss.jpg
in experience wikimedia images fail download reason.
i see no way of tracking download progress using httpclient. there way so?
edit: seems setting timeout not have function. httprequestexception not thrown after 5 seconds.
edit2: tried different approach, 1 anonshankar suggested. method code stuck @ line:
byte[] img = response.content.readasbytearrayasync();
so httpresponse arrives, somehow bytes not read out, no matter how much time gave it. how happen? hard part getting response, reading out bytes should simple.
again, happens images, of them downloads correctly. 1 illustration mentioned above.
i have modified image downloader code, times out after few seconds. here final code:
internal static async task<bool> download_wallpaper(string image_url, string file_name, string destination_folder_name) { seek { using (var client = new httpclient()) { // prevent running out of memory client.maxresponsecontentbuffersize = devicestatus.applicationmemoryusagelimit / 3; byte[] image_byte_arr = null; using (cancellationtokensource cts = new cancellationtokensource()) { var task = task.factory.startnew(() => { seek { image_byte_arr = client.getbytearrayasync(image_url).result; } grab (aggregateexception x)// handling read errors, image big { console.writeline(x.message); foreach (var v in x.innerexceptions) console.writeline(v.message); image_byte_arr = null; } }, cts.token); bool finished_in_time = task.wait(timespan.fromseconds(5)); if (!finished_in_time)// timeout { cts.cancel(); task.wait(); homecoming false; } else if (image_byte_arr == null)// read error { homecoming false; } } var folder = await storagefolder.getfolderfrompathasync(destination_folder_name); // create file storagefile file = await folder.createfileasync(file_name, creationcollisionoption.replaceexisting); using (var write_stream = await file.openstreamforwriteasync()) { write_stream.write(image_byte_arr, 0, image_byte_arr.length); } console.writeline(devicestatus.applicationcurrentmemoryusage); homecoming true; } } grab (httprequestexception x) { console.writeline(x); homecoming false; } grab (outofmemoryexception x) { gc.collect(); homecoming false; } grab (exception x) { console.writeline(x); homecoming false; } }
any improvement suggestions welcome, , still don't understand why method httpcontent.readasbytearrayasync() gets stuck.
just seek out snippet
worked me.
httpclient client = new httpclient(); httpresponsemessage response = await client.getasync("give url"); byte[] img = response.content.readasbytearray(); inmemoryrandomaccessstream randomaccessstream = new inmemoryrandomaccessstream(); datawriter author = new datawriter(randomaccessstream.getoutputstreamat(0)); writer.writebytes(img); await writer.storeasync(); bitmapimage b = new bitmapimage(); b.setsource(randomaccessstream); pic.source = b; //(pic `<image>` defined in `xaml`
hope helps!
windows-phone-8 dotnet-httpclient
No comments:
Post a Comment