android - How to download a file on a click -


how doing ? making application, in application want click listview , application starts download link. have link. used following code but, application crashes:

here listener handle clicks:

mlistviewmagazine.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position,                                 long id) {             downloadfromurl(mmagazine_title[position]);           }     }); 

the downloadfromurl function following:

public void downloadfromurl( string filename) {  //this downloader method     try {         url url = new url("http://themwl.org/web/sites/default/files/584_0.pdf");                 file file = new file(filename);          long starttime = system.currenttimemillis();         log.d("imagemanager", "download begining");         log.d("imagemanager", "download url:" + url);         log.d("imagemanager", "downloaded file name:" + filename);                     /* open connection url. */         urlconnection ucon = url.openconnection();                      /*                      * define inputstreams read urlconnection.                      */         inputstream = ucon.getinputstream();         bufferedinputstream bis = new bufferedinputstream(is);                      /*                      * read bytes buffer until there nothing more read(-1).                      */         bytearraybuffer baf = new bytearraybuffer(50);         int current = 0;         while ((current = bis.read()) != -1) {             baf.append((byte) current);         }                      /* convert bytes read string. */         fileoutputstream fos = new fileoutputstream(file);         fos.write(baf.tobytearray());         fos.close();         log.d("imagemanager", "download ready in"                 + ((system.currenttimemillis() - starttime) / 1000)                 + " sec");      } catch (ioexception e) {         log.d("imagemanager", "error: " + e);     }  } 

the error message:

 06-19 22:32:01.585  12336-12336/alahdal.amjad.mwlapplication e/androidruntime﹕ fatal exception: main process: alahdal.amjad.mwlapplication, pid: 12336 android.os.networkonmainthreadexception         @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1147)         @ java.net.inetaddress.lookuphostbyname(inetaddress.java:418)         @ java.net.inetaddress.getallbynameimpl(inetaddress.java:252)         @ java.net.inetaddress.getallbyname(inetaddress.java:215)         @ com.android.okhttp.hostresolver$1.getallbyname(hostresolver.java:29)         @ com.android.okhttp.internal.http.routeselector.resetnextinetsocketaddress(routeselector.java:232)         @ com.android.okhttp.internal.http.routeselector.next(routeselector.java:124)         @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:272)         @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:211)         @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:373)         @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:323)         @ com.android.okhttp.internal.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:190)         @ alahdal.amjad.mwlapplication.publicationsfragment.downloadfromurl(publicationsfragment.java:98)         @ alahdal.amjad.mwlapplication.publicationsfragment$1.onitemclick(publicationsfragment.java:75)         @ android.widget.adapterview.performitemclick(adapterview.java:300)         @ android.widget.abslistview.performitemclick(abslistview.java:1143)         @ android.widget.abslistview$performclick.run(abslistview.java:3044)         @ android.widget.abslistview$3.run(abslistview.java:3833)         @ android.os.handler.handlecallback(handler.java:739)         @ android.os.handler.dispatchmessage(handler.java:95)         @ android.os.looper.loop(looper.java:135)         @ android.app.activitythread.main(activitythread.java:5221)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 

this solution:

            downloadmanager.request request = new downloadmanager.request(uri.parse(files_url[position]));             request.settitle("file downloading");             request.setdescription("file being downloaded...");             request.allowscanningbymediascanner();             request.setnotificationvisibility(downloadmanager.request.visibility_visible_notify_completed);             request.setnotificationvisibility(downloadmanager.request.visibility_visible);             string file_name = urlutil.guessfilename(files_url[position],null, mimetypemap.getfileextensionfromurl(files_url[position]));             request.setdestinationinexternalpublicdir(environment.directory_downloads,file_name)  ;             downloadmanager manager = (downloadmanager) getactivity().getsystemservice(context.download_service);             manager.enqueue(request);             toast.maketext(getactivity(),"downloading",toast.length_long).show(); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -