首页 > 代码库 > android download学习记录

android download学习记录

东西拼凑,终于弄出来可以用的代码

[1].[代码] [Java]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
publicclassDownManagerTask {
   
   privatestaticfinal String TAG = "DownManagerTask";
   
   privatestaticDownManagerTask DOWN_TASK = newDownManagerTask();
   
   privateContext context;
   privateDownloadManager downloadManager;
   longdownloadId = -1;
   privateBitmap bitmap;
   privateImageView imageView;
   
   
   
   privateDownManagerTask(){}
   
   publicstaticDownManagerTask getInstance(){
       returnDOWN_TASK;
   }
   
   @SuppressLint("InlinedApi")
   publicvoidinit(Context context,ImageView imageView){
       this.context = context;
       this.imageView = imageView;
       downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
   }
   
   /**
    * 将下载的图片转化成bitMap
    *
    */
   @SuppressLint("NewApi")
   privatevoidparseBitMap(){
       try{
           ParcelFileDescriptor pfd = downloadManager.openDownloadedFile(downloadId);
           FileDescriptor fileDescriptor = pfd.getFileDescriptor(); 
           bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); 
           pfd.close();
       }catch(Exception e){ 
           e.printStackTrace();
       }
   }
   
   /**
    * 下载方法
    * @param url
    */
   @SuppressLint("NewApi")
   publicvoiddownload(String url){
       Log.d(TAG, url);
       Uri uri = Uri.parse(url);
       DownloadManager.Request request = newDownloadManager.Request(uri);
       request.setAllowedNetworkTypes(Request.NETWORK_MOBILE
               | Request.NETWORK_WIFI);
       request.setAllowedOverRoaming(false);
       MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
       String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap
               .getFileExtensionFromUrl(url));
       request.setMimeType(mimeString);
       //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Bugdroid.png"); 
       request.allowScanningByMediaScanner();
       request.setVisibleInDownloadsUi(true);
       downloadId = downloadManager.enqueue(request);
       Log.d(TAG,"downloadId:"+downloadId);
       context.registerReceiver(receiver,newIntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
   }
   
   publicvoidunregisterReceiver(){
       context.unregisterReceiver(receiver);
   }
   
   @SuppressLint("NewApi")
   publicvoidtemp(){
       Cursor c = downloadManager.query(newDownloadManager.Query().setFilterById(downloadId));
       if(c != null){
           c.moveToFirst();
           Log.d(TAG,(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME))));
           c.close();
       }
   }
   
   privateBroadcastReceiver receiver = newBroadcastReceiver() {
       
       @SuppressLint("NewApi")
       @Override
       publicvoidonReceive(Context context, Intent intent) {
           //这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听
           if(intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
               Log.v(TAG,""+intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0));
                Log.v(TAG,"download complete");
                parseBitMap();
                imageView.setImageBitmap(bitmap);
                Toast.makeText(context,"download complete", Toast.LENGTH_SHORT).show();
           }
       }
   };
   

android download学习记录