afinal Loading network pictures and downloading file usage

Source: Internet
Author: User

The Afinal high-speed development framework is easy to use. The following explains how to use afinal to load Web images and download files:

Look first:


Note: To add afinal jar before using afinal , you can download it here: http://download.csdn.net/detail/baiyuliang2013/7313587

It contains the source code and jar of the afinal, which only needs to be added to the jar package. You can also add source code directly to your project during the learning phase. Be able to learn afinal in more depth .


Look at the code:

Activit_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <button android:id=" @+id/btn "android:layout_width=" Wrap_content "android:l ayout_height= "wrap_content" android:text= "Download"/> <textview android:id= "@+id/text" an        Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_torightof= "@+id/btn" android:text= "Progress:"/> <imageview android:id= "@+id/img" android:layout_width= "Wrap_conten T "android:layout_height=" Wrap_content "android:layout_centerinparent=" true "Android:layout_centerhor Izontal= "true" android:src= "@drawable/ic_launcher"/></relativelayout>

Mainactivity.java:

Package Com.example.afinaltest;import Java.io.file;import Net.tsz.afinal.*;import Net.tsz.afinal.annotation.view.viewinject;import Net.tsz.afinal.http.ajaxcallback;import Android.annotation.suppresslint;import Android.os.bundle;import Android.os.environment;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Finalactivity {@ViewInject (id= R.ID.IMG) ImageView img; TextView TextView; Button btn; Finalbitmap Finalbitmap=null; Finalhttp fh; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); textview= (TextView) Findviewbyid (R.id.text); btn= (Button) Findviewbyid ( R.ID.BTN); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {String Apkpath = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/qq.apk"; File f = new File (Apkpath), if (F.exists ()) {F.delete ();} Fh=new finalhttp (); Fh.download ("http://gdown.baidu.com/data/wisegame/4ae6d2d7378e6cdf/QQ_122.apk", Apkpath, new Ajaxcallback<file> () {@Overridepublic void OnStart () {Super.onstart (); Toast.maketext (Getapplicationcontext (), "Start Download", Toast.length_short). Show (); @SuppressLint ("Defaultlocale") @Overridepublic void onloading (Long count, long current) {super.onloading (count,  current), int progress=0;if (current! = Count && Current! = 0) {progress = (int) (Current/(float) count * 100);} else {progress = 100;} Textview.settext ("Progress:" +progress+ "%");} @Overridepublic void onsuccess (File t) {super.onsuccess (t); Toast.maketext (Getapplicationcontext (), "Download Complete", Toast.length_short). Show (); Textview.settext (t==null? " Null ": T.getabsolutefile (). toString ());} @Overridepublic void OnFailure (Throwable t, int errorno,string strMsg) {super.onfailure (T, Errorno, STRMSG); Toast.maketext (Getapplicationcontext (), "Download Failed", Toast.length_short). Show ();});}}); Finalbitmap=finalbitmap.create (Mainactivity.this); Finalbitmap.display (IMG, "http://meme.zenfs.com/u/ A33312d2e9eaa443321f4ec716fe795a23c27c89.jpeg ");}}
Can see. Mainactivity is inherited by Fianlactivity, so when the control is initialized. Can not be findviewbyid, directly with the way of annotations such as: @ViewInject (ID=R.ID.IMG) ImageView img; The @ViewInject (id=r.id.btn,click= "onclick") Button Btn;click event simply writes a correspondingclick= The "onclick" method, such as: public void onclick () {} is OK.

analyzing the Finallybitmap source code shows that the Create method needs to be called when initializing. And then call display (Imgview,url), you can load the network picture. You can also set the image displayed before loading, and so on, this look at the source code will know. The download file requires finalhttp and calls the download method, which uses the download (string,string,ajaxcallback<file>) method. The first parameter is the file path that will be downloaded. The second parameter is the local file save path, the third is a callback function, every second will be called once, can easily view the file download progress and file download situation such as successful failure, download, etc., need to rewrite onstart,onloading,onsuccess, OnFailure and other methods.


Finally, don't forget to include network access and memory card read and Write permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>



afinal Loading network pictures and downloading file usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.