Afinal加载网络图片及下载文件使用方法
来源:互联网 发布:可用的数据接口 编辑:程序博客网 时间:2024/05/29 04:37
Afinal快速开发框架使用起来非常方便,下面将讲解如何利用Afinal加载网络图片及下载文件:
先看效果图:
注意:使用Afinal前需添加Afinal的jar,可以在这里下载:http://download.csdn.net/detail/baiyuliang2013/7313587
其中包括了Afinal的源码和jar,使用时只需添加jar包即可,在学习阶段也可以直接添加源码到你的项目中,可以更深入的学习Afinal。
看代码:
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:layout_height="wrap_content"
- android:text="下载" />
-
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@+id/btn"
- android:text="进度:" />
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_centerHorizontal="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;
-
- @Override
- protected 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() {
- @Override
- public 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>() {
- @Override
- public void onStart() {
- super.onStart();
- Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
- }
- @SuppressLint("DefaultLocale")
- @Override
- public 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+"%");
- }
- @Override
- public void onSuccess(File t) {
- super.onSuccess(t);
- Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
- textView.setText(t==null?"null":t.getAbsoluteFile().toString());
- }
- @Override
- public void onFailure(Throwable t, int errorNo,String strMsg) {
- super.onFailure(t, errorNo, strMsg);
- Toast.makeText(getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
-
- finalBitMap=FinalBitmap.create(MainActivity.this);
-
- finalBitMap.display(img, "http://meme.zenfs.com/u/a33312d2e9eaa443321f4ec716fe795a23c27c89.jpeg");
-
- }
-
-
- }
可以看到,MainActivity是继承了FianlActivity,因此在初始化控件的时候,可以不用findViewById了,直接用注解的方式如:@ViewInject(id=R.id.img) ImageView img; @ViewInject(id=R.id.btn,click="onclick") Button btn;click事件只需写一个对应click="onclick"的方法如:public void onclick(){}即可。分析FinallyBitmap源码可知:初始化时需调用create方法,然后再调用display(imgView,url);即可加载网络图片,也可以设置在加载完成前显示的图片等等,这个看下源代码便知,下载文件需用FinalHttp,并调用download方法,该例中使用download(String,String,AjaxCallBack<File>);方法,第一个参数是将要下载的文件路径,第二个参数为本地文件保存路径,第三个是一个回调函数,每隔一秒会调用一次,可以方便的查看文件下载进度以及文件下载情况如成功失败,下载完成等,需重写onStart,onLoading,onSuccess,onFailure等方法。
最后不要忘了在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" />
http://t.163.com/event/info/eventId/-5027698643573193717http://t.163.com/event/info/eventId/-305053728689549265http://t.163.com/event/info/eventId/-7759517914263665940http://t.163.com/event/info/eventId/7519356754209113530http://t.163.com/event/info/eventId/-1317711063123950830http://t.163.com/event/info/eventId/-5812555017773771644http://t.163.com/event/info/eventId/-920708692776070194http://t.163.com/event/info/eventId/5142033136039344551http://t.163.com/event/info/eventId/-5924238123315621049http://t.163.com/event/info/eventId/-3741593273440369816http://t.163.com/event/info/eventId/5832903343121024645http://t.163.com/event/info/eventId/-183169997833000215http://t.163.com/event/info/eventId/-6582210172882160659http://t.163.com/event/info/eventId/3103105559872343083http://t.163.com/event/info/eventId/-5259470010418590631http://t.163.com/event/info/eventId/3946297762708477066http://t.163.com/event/info/eventId/660961031911809424http://t.163.com/event/info/eventId/-7224093148547152505http://t.163.com/event/info/eventId/5698904029032417109http://t.163.com/event/info/eventId/4639333300696595986http://t.163.com/event/info/eventId/6151939625726442318http://t.163.com/event/info/eventId/-250567828286264555http://t.163.com/event/info/eventId/-3015472448602657740http://t.163.com/event/info/eventId/2761461602466778720http://t.163.com/event/info/eventId/-5637673156858535950http://t.163.com/event/info/eventId/2313835453498288512http://t.163.com/event/info/eventId/5906672506892550590http://t.163.com/event/info/eventId/-2625707016128417342http://t.163.com/event/info/eventId/-8889015302349115780http://t.163.com/event/info/eventId/-3189219818972249970http://t.163.com/event/info/eventId/-164245576116072643http://t.163.com/event/info/eventId/-3708493243249077392http://t.163.com/event/info/eventId/6071226022880240586http://t.163.com/event/info/eventId/-134834713388923628http://t.163.com/event/info/eventId/8265951826233784812http://t.163.com/event/info/eventId/-7535893756434898014http://t.163.com/event/info/eventId/-7298062085188339511http://t.163.com/event/info/eventId/258755492199305028http://t.163.com/event/info/eventId/-8598301783874564672http://t.163.com/event/info/eventId/6100308171905490337http://t.163.com/event/info/eventId/4429782957715889808http://t.163.com/event/info/eventId/-1727997745022085303http://t.163.com/event/info/eventId/8251521842322087182http://t.163.com/event/info/eventId/3309603840128714672http://t.163.com/event/info/eventId/-8265370640845654886http://t.163.com/event/info/eventId/-1499566034136539396http://t.163.com/event/info/eventId/-3176859542726242215http://t.163.com/event/info/eventId/-5297617163399403458http://t.163.com/event/info/eventId/-412697380000310322http://t.163.com/event/info/eventId/7142021704779303571http://t.163.com/event/info/eventId/7475170569421166450http://t.163.com/event/info/eventId/7947183949043382432http://t.163.com/event/info/eventId/-3189338326043951620http://t.163.com/event/info/eventId/-7592768591129894301http://t.163.com/event/info/eventId/711421964119848239http://t.163.com/event/info/eventId/8159156252902887190http://t.163.com/event/info/eventId/-4660094699010671598http://t.163.com/event/info/eventId/8483449775013797793http://t.163.com/event/info/eventId/1254784800270924566http://t.163.com/event/info/eventId/8544282454023933756http://t.163.com/event/info/eventId/1920385652813640687http://t.163.com/event/info/eventId/-2758641790651340667http://t.163.com/event/info/eventId/707768686185191686http://t.163.com/event/info/eventId/602478054679704913http://t.163.com/event/info/eventId/594966127240680774http://t.163.com/event/info/eventId/-8092904767390886057http://t.163.com/event/info/eventId/8227137346582128210http://t.163.com/event/info/eventId/-5912333673260979381http://t.163.com/event/info/eventId/5014642047137222495http://t.163.com/event/info/eventId/-6372517780114686424http://t.163.com/event/info/eventId/-2260566304212859391http://t.163.com/event/info/eventId/3260499478366215000http://t.163.com/event/info/eventId/6756391510678283265http://t.163.com/event/info/eventId/8294397381659753712http://t.163.com/event/info/eventId/-6269163382705031612http://t.163.com/event/info/eventId/510878920528635935http://t.163.com/event/info/eventId/-6400911842577534577http://t.163.com/event/info/eventId/8010006015931948963http://t.163.com/event/info/eventId/861270319414752863http://t.163.com/event/info/eventId/5356502297656732867http://t.163.com/event/info/eventId/2703085706845772077http://t.163.com/event/info/eventId/1809460863862973875http://t.163.com/event/info/eventId/8394108879215852167http://t.163.com/event/info/eventId/8770060792347152937http://t.163.com/event/info/eventId/-6821338703249539382http://t.163.com/event/info/eventId/7939276357019688031http://t.163.com/event/info/eventId/-8500617915075092210http://t.163.com/event/info/eventId/-2684691995090064033http://t.163.com/event/info/eventId/-6671134027740337394http://t.163.com/event/info/eventId/-2612212749735025713http://t.163.com/event/info/eventId/7529582577470352474http://t.163.com/event/info/eventId/-6648465066274318979http://t.163.com/event/info/eventId/-219303066078441405http://t.163.com/event/info/eventId/-6844349464328229641http://t.163.com/event/info/eventId/-9221261804615312504http://t.163.com/event/info/eventId/-3582559042743976173http://t.163.com/event/info/eventId/817208785158260489http://t.163.com/event/info/eventId/8892162128794392098http://t.163.com/event/info/eventId/9024236091254883182http://t.163.com/event/info/eventId/1356947596921598430http://t.163.com/event/info/eventId/4291957022227829769http://t.163.com/event/info/eventId/43625134913150676http://t.163.com/event/info/eventId/-8490272869137613546http://t.163.com/event/info/eventId/-5773715220500713556http://t.163.com/event/info/eventId/-1276172528113285360http://t.163.com/event/info/eventId/4917498762682851834http://t.163.com/event/info/eventId/-1733773104745310397http://t.163.com/event/info/eventId/6669889803619291637http://t.163.com/event/info/eventId/-7082140643930188839http://t.163.com/event/info/eventId/-1395191818494318001http://t.163.com/event/info/eventId/-3859422258990922561http://t.163.com/event/info/eventId/7833703076769988177http://t.163.com/event/info/eventId/-7353210558236035774http://t.163.com/event/info/eventId/4071466032009806441http://t.163.com/event/info/eventId/6411680759979551803http://t.163.com/event/info/eventId/991665348972604288http://t.163.com/event/info/eventId/-716201121610835763http://t.163.com/event/info/eventId/5420426237973782858http://t.163.com/event/info/eventId/-7655137793137111994http://t.163.com/event/info/eventId/-8716499189494078024http://t.163.com/event/info/eventId/-4903719637443328903http://t.163.com/event/info/eventId/-950719266525781605http://t.163.com/event/info/eventId/-5935953496175442312http://t.163.com/event/info/eventId/-7065226388155691743http://t.163.com/event/info/eventId/2556917612492534181http://t.163.com/event/info/eventId/-8847906841270746401http://t.163.com/event/info/eventId/-1071020685616590826http://t.163.com/event/info/eventId/2317800852308909414http://t.163.com/event/info/eventId/-6952076772833393836http://t.163.com/event/info/eventId/2166425804662546461http://t.163.com/event/info/eventId/-6685989285417717911http://t.163.com/event/info/eventId/-7983508971115578855http://t.163.com/event/info/eventId/1699309870374706931http://t.163.com/event/info/eventId/-969831640259831039http://t.163.com/event/info/eventId/-7564406978600101657http://t.163.com/event/info/eventId/-7210687587739096232http://t.163.com/event/info/eventId/2054341241076723863http://t.163.com/event/info/eventId/-4057789561699380801http://t.163.com/event/info/eventId/3008879249403901020http://t.163.com/event/info/eventId/7604779822682947795http://t.163.com/event/info/eventId/2717114927751137665http://t.163.com/event/info/eventId/-3079722707629863240http://t.163.com/event/info/eventId/-8741754173682296372http://t.163.com/event/info/eventId/-3842507892811761509http://t.163.com/event/info/eventId/-2717646594678136358http://t.163.com/event/info/eventId/2821014323669429886http://t.163.com/event/info/eventId/7515746557498107693http://t.163.com/event/info/eventId/5735792347170438597http://t.163.com/event/info/eventId/-6398138266326370212http://t.163.com/event/info/eventId/5899497333195461824http://t.163.com/event/info/eventId/-7155854987711105457http://t.163.com/event/info/eventId/2147804975032912510http://t.163.com/event/info/eventId/-8938823285946156684http://t.163.com/event/info/eventId/-6539337728831280589http://t.163.com/event/info/eventId/6258933729550287829http://t.163.com/event/info/eventId/255147624671667358http://t.163.com/event/info/eventId/-1209624547839197140http://t.163.com/event/info/eventId/7181224507291516865http://t.163.com/event/info/eventId/-8899526651424610416http://t.163.com/event/info/eventId/4231742603570164093http://t.163.com/event/info/eventId/3346429867613528442http://t.163.com/event/info/eventId/2362536588927495682http://t.163.com/event/info/eventId/1856290682163247744http://t.163.com/event/info/eventId/637128019723827275http://t.163.com/event/info/eventId/-8583978117917781499http://t.163.com/event/info/eventId/-7139988019443519694http://t.163.com/event/info/eventId/-4706429532553648801http://t.163.com/event/info/eventId/8918114390312647404http://t.163.com/event/info/eventId/-4758042170442308275http://t.163.com/event/info/eventId/3199667060005287999http://t.163.com/event/info/eventId/-1126058476924584750http://t.163.com/event/info/eventId/-3591245823202262893http://t.163.com/event/info/eventId/-5323400288613228525http://t.163.com/event/info/eventId/-3800100364645279837http://t.163.com/event/info/eventId/-8983406729755856870http://t.163.com/event/info/eventId/-3119323392065631142http://t.163.com/event/info/eventId/-3256105097364407857http://t.163.com/event/info/eventId/-7138908737923239814http://t.163.com/event/info/eventId/2378206407345792493http://t.163.com/event/info/eventId/-330478534712757921http://t.163.com/event/info/eventId/643954840270792600http://t.163.com/event/info/eventId/7696621046808128775http://t.163.com/event/info/eventId/4626395594347494334http://t.163.com/event/info/eventId/-8988790468691628739http://t.163.com/event/info/eventId/-1908701874026045421http://t.163.com/event/info/eventId/3710418041903287737http://t.163.com/event/info/eventId/-17635867257645799http://t.163.com/event/info/eventId/-7083629830840074468http://t.163.com/event/info/eventId/8313483403831228893http://t.163.com/event/info/eventId/3033269495226866211http://t.163.com/event/info/eventId/-498168076205506659http://t.163.com/event/info/eventId/-8997475919658928338http://t.163.com/event/info/eventId/5633581331567546698http://t.163.com/event/info/eventId/3443129983734037248http://t.163.com/event/info/eventId/5121667276815273773http://t.163.com/event/info/eventId/-1972208489908144473http://t.163.com/event/info/eventId/2422273710665068633http://t.163.com/event/info/eventId/4133666506218220587http://t.163.com/event/info/eventId/-1371648897437444464http://t.163.com/event/info/eventId/4836908328996503871http://t.163.com/event/info/eventId/4963841255239727145 0 0