Android中文件的多點續傳,開源框架afinal

博客原址:http://write.blog.csdn.net/postedit

源碼地址:https://github.com/yangfuhai/afinal

Afinal簡介

  • Afinal 是一個android的sqlite orm 和 ioc 框架。同時封裝了android中的http框架,使其更加簡單易用;
  • 使用finalBitmap,無需考慮bitmap在android中加載的時候oom的問題和快速滑動的時候圖片加載位置錯位等問題。
  • Afinal的宗旨是簡潔,快速。約定大於配置的方式。儘量一行代碼完成所有事情。

目前Afinal主要有四大模塊:

  • FinalDB模塊:android中的orm框架,一行代碼就可以進行增刪改查。支持一對多,多對一等查詢。

  • FinalActivity模塊:android中的ioc框架,完全註解方式就可以進行UI綁定和事件綁定。無需findViewById和setClickListener等。

  • FinalHttp模塊:通過httpclient進行封裝http數據請求,支持ajax方式加載。

  • FinalBitmap模塊:通過FinalBitmap,imageview加載bitmap的時候無需考慮bitmap加載過程中出現的oom和android容器快速滑動時候出現的圖片錯位等現象。FinalBitmap可以配置線程加載線程數量,緩存大小,緩存路徑,加載顯示動畫等。FinalBitmap的內存管理使用lru算法,沒有使用弱引用(android2.3以後google已經不建議使用弱引用,android2.3後強行回收軟引用和弱引用,詳情查看android官方文檔),更好的管理bitmap內存。FinalBitmap可以自定義下載器,用來擴展其他協議顯示網絡圖片,比如ftp等。同時可以自定義bitmap顯示器,在imageview顯示圖片的時候播放動畫等(默認是漸變動畫顯示)。


使用afinal快速開發框架需要有以下權限:

1 <uses-permission android:name="android.permission.INTERNET" />
2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • 第一個是訪問網絡
  • 第二個是訪問sdcard
  • 訪問網絡是請求網絡圖片的時候需要或者是http數據請求時候需要,訪問sdcard是圖片緩存的需要。

FinalDB使用方法

關於finalDb的更多介紹,請點擊這裏

1 FinalDb db = FinalDb.create(this);
2 User user = new User(); //這裏需要注意的是User對象必須有id屬性,或者有通過@ID註解的屬性
3 user.setEmail("[email protected]");
4 user.setName("michael yang");
5 db.save(user);

FinalActivity使用方法:

  • 完全註解方式就可以進行UI綁定和事件綁定
  • 無需findViewById和setClickListener等

01 public class AfinalDemoActivity extends FinalActivity {
02  
03     //無需調用findViewById和setOnclickListener等
04     @ViewInject(id=R.id.button,click="btnClick") Button button;
05     @ViewInject(id=R.id.textView) TextView textView;
06  
07     public void onCreate(Bundle savedInstanceState) {
08        super.onCreate(savedInstanceState);
09        setContentView(R.layout.main);
10     }
11  
12     public void btnClick(View v){
13        textView.setText("text set form button");
14     }
15 }

FinalHttp使用方法:

普通get方法

01 FinalHttp fh = new FinalHttp();
02 fh.get("http://www.yangfuhai.com"new AjaxCallBack(){
03  
04     @Override
05     public void onLoading(long count, long current) { //每1秒鐘自動被回調一次
06             textView.setText(current+"/"+count);
07     }
08  
09     @Override
10     public void onSuccess(String t) {
11             textView.setText(t==null?"null":t);
12     }
13  
14     @Override
15     public void onStart() {
16         //開始http請求的時候回調
17     }
18  
19     @Override
20     public void onFailure(Throwable t, String strMsg) {
21         //加載失敗的時候回調
22     }
23 });

使用FinalHttp上傳文件 或者 提交數據 到服務器(post方法)

文件上傳到服務器,服務器如何接收,請查看這裏

01 AjaxParams params = new AjaxParams();
02   params.put("username""michael yang");
03   params.put("password""123456");
04   params.put("email""[email protected]");
05   params.put("profile_picture"new File("/mnt/sdcard/pic.jpg")); // 上傳文件
06   params.put("profile_picture2", inputStream); // 上傳數據流
07   params.put("profile_picture3"new ByteArrayInputStream(bytes)); // 提交字節流
08  
09   FinalHttp fh = new FinalHttp();
10   fh.post("http://www.yangfuhai.com", params, new AjaxCallBack(){
11         @Override
12         public void onLoading(long count, long current) {
13                 textView.setText(current+"/"+count);
14         }
15  
16         @Override
17         public void onSuccess(String t) {
18             textView.setText(t==null?"null":t);
19         }
20   });

使用FinalHttp下載文件:

  • 支持斷點續傳,隨時停止下載任務 或者 開始任務
01 FinalHttp fh = newFinalHttp(); 
02     //調用download方法開始下載
03     HttpHandler handler = fh.download("http://www.xxx.com/下載路徑/xxx.apk", //這裏是下載的路徑
04     true,//true:斷點續傳 false:不斷點續傳(全新下載)
05     "/mnt/sdcard/testapk.apk"//這是保存到本地的路徑
06     newAjaxCallBack() { 
07                 @Override 
08                 public void onLoading(long count, longcurrent) { 
09                      textView.setText("下載進度:"+current+"/"+count); 
10                 
11  
12                 @Override 
13                 public voidonSuccess(File t) { 
14                     textView.setText(t==null?"null":t.getAbsoluteFile().toString()); 
15                 
16  
17             }); 
18  
19  
20    //調用stop()方法停止下載
21    handler.stop();

FinalBitmap 使用方法

加載網絡圖片就一行代碼 fb.display(imageView,url) ,更多的display重載請看幫助文檔

01 private GridView gridView;
02     private FinalBitmap fb;
03     @Override
04     protected void onCreate(Bundle savedInstanceState) {
05         super.onCreate(savedInstanceState);
06         setContentView(R.layout.images);
07  
08         gridView = (GridView) findViewById(R.id.gridView);
09         gridView.setAdapter(mAdapter);
10  
11         fb = FinalBitmap.create(this);//初始化FinalBitmap模塊
12         fb.configLoadingImage(R.drawable.downloading);
13         //這裏可以進行其他十幾項的配置,也可以不用配置,配置之後必須調用init()函數,才生效
14         //fb.configBitmapLoadThreadSize(int size)
15         //fb.configBitmapMaxHeight(bitmapHeight)
16     }
17  
18  
19 ///////////////////////////adapter getView////////////////////////////////////////////
20  
21 public View getView(int position, View convertView, ViewGroup parent) {
22     ImageView iv;
23     if(convertView == null){
24         convertView = View.inflate(BitmapCacheActivity.this,R.layout.image_item, null);
25         iv = (ImageView) convertView.findViewById(R.id.imageView);
26         iv.setScaleType(ScaleType.CENTER_CROP);
27         convertView.setTag(iv);
28     }else{
29         iv = (ImageView) convertView.getTag();
30     }
31     //bitmap加載就這一行代碼,display還有其他重載,詳情查看源碼
32     fb.display(iv,Images.imageUrls[position]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章