java - Downloading Image to SD Card in Service fails (Android) -
i created separate project , activity downloads image url, converts bitmap, , uses fileoutputstream save file sd card. when in separate project , free-standing activity, worked fine, , see image stored on sd card.
public class picturedownload extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); downloadfromurl("http://www.coutblog.com/prism/uploads/0030.jpg", "newpic.jpg"); displaypic("newpic.jpg"); } // place download file private final string path = "/sdcard/"; public void displaypic(string filename) { imageview image = (imageview)findviewbyid(r.id.image); bitmap bmap = bitmapfactory.decodefile(path + filename); image.setimagebitmap(bmap); } /** * downloads picture url sd card * @param imageurl: url download (absolute web url) * @param filename: name of file want save picture */ public void downloadfromurl(string imageurl, string filename) { try { // connect url url myimageurl = new url(imageurl); httpurlconnection connection = (httpurlconnection)myimageurl.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); // bitmap bitmap mybitmap = bitmapfactory.decodestream(input); // save bitmap file string path = environment.getexternalstoragedirectory().tostring(); outputstream fout = null; file file = new file(path, filename); fout = new fileoutputstream(file); mybitmap.compress(bitmap.compressformat.jpeg, 85, fout); fout.flush(); fout.close(); } catch (ioexception e) {} }
}
but, whenever tried port code existing project, fails. in setting, running code service. code exact same - reason won't download files. have fact running on service instead of activity? or maybe reason file output stream gets messed up.
in bgservice:
string name = remote_resource.getvalue().substring(38); downloadfromurl(remote_resource.getvalue(), name); /** * downloads picture url sd card * @param imageurl: url download (absolute web url) * @param filename: name of file want save picture */ public void downloadfromurl(string imageurl, string filename) { try { // connect url url myimageurl = new url(imageurl); httpurlconnection connection = (httpurlconnection)myimageurl.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); // bitmap bitmap mybitmap = bitmapfactory.decodestream(input); // save bitmap file string path = environment.getexternalstoragedirectory().tostring(); outputstream fout = null; file file = new file(path, filename); log.i("help", file.getabsolutepath()); log.i("help", file.tostring()); log.i("help", file.length() + ""); system.out.println("this test of system.out.println()"); fout = new fileoutputstream(file); mybitmap.compress(bitmap.compressformat.jpeg, 85, fout); fout.flush(); fout.close(); system.out.println("file path: " + file.getabsolutepath()); log.i("help2", file.getabsolutepath()); log.i("help2", file.tostring()); log.i("help2", file.length() + ""); } catch (ioexception e) {} }
another interesting thing of "log.i" calls below, second 1 (below fout.flush()) gets printed. somewhere in bitmap compression or fileoutputstream io weird happening. no exceptions thrown.
thanks lot help!
are sure have write_external_storage enabled. if using permission-group.storage doesn't include in there.
Comments
Post a Comment