hey guys can tell me why cannot save file external sd? can check on code?
public void download() { try { //this file want download remote server string path ="http://mozilla.cdn.leaseweb.com/firefox/releases/4.0.1/win32/en-us/firefox%20setup%204.0.1.exe"; //this name of local file create string targetfilename; boolean eof = false; url u = new url(path); httpurlconnection c = (httpurlconnection) u.openconnection(); c.setrequestmethod("get"); c.setdooutput(true); c.connect(); //string svto = environment.getexternalstoragestate().tostring(); file path1 = environment.getexternalstoragepublicdirectory( environment.directory_pictures); path1.mkdirs(); fileoutputstream f = new fileoutputstream(new file(path1+"/fox.exe")); inputstream in = c.getinputstream(); byte[] buffer = new byte[1024]; int len1 = 0; while ( (len1 = in.read(buffer)) != -1 ) { f.write(buffer,0, len1); } f.close(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (protocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
this operation fail if don't have following permissions in androidmanifest.xml
file:
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.internet" />
make sure put them within top-level <manifest>
tag, not <application>
tag activities specified.
Comments
Post a Comment