博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android download学习记录
阅读量:5077 次
发布时间:2019-06-12

本文共 3049 字,大约阅读时间需要 10 分钟。

东西拼凑,最终弄出来能够用的代码

[1].[代码] [Java]代码

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
public
class
DownManagerTask {
   
   
private
static
final
String TAG =
"DownManagerTask"
;
   
   
private
static
DownManagerTask DOWN_TASK =
new
DownManagerTask();
   
   
private
Context context;
   
private
DownloadManager downloadManager;
   
long
downloadId = -
1
;
   
private
Bitmap bitmap;
   
private
ImageView imageView;
   
   
   
   
private
DownManagerTask(){}
   
   
public
static
DownManagerTask getInstance(){
       
return
DOWN_TASK;
   
}
   
   
@SuppressLint
(
"InlinedApi"
)
   
public
void
init(Context context,ImageView imageView){
       
this
.context = context;
       
this
.imageView = imageView;
       
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
   
}
   
   
/**
    
* 将下载的图片转化成bitMap
    
*
    
*/
   
@SuppressLint
(
"NewApi"
)
   
private
void
parseBitMap(){
       
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"
)
   
public
void
download(String url){
       
Log.d(TAG, url);
       
Uri uri = Uri.parse(url);
       
DownloadManager.Request request =
new
DownloadManager.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,
new
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
   
}
   
   
public
void
unregisterReceiver(){
       
context.unregisterReceiver(receiver);
   
}
   
   
@SuppressLint
(
"NewApi"
)
   
public
void
temp(){
       
Cursor c = downloadManager.query(
new
DownloadManager.Query().setFilterById(downloadId));
       
if
(c !=
null
){
           
c.moveToFirst();
           
Log.d(TAG,(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME))));
           
c.close();
       
}
   
}
   
   
private
BroadcastReceiver receiver =
new
BroadcastReceiver() {
       
       
@SuppressLint
(
"NewApi"
)
       
@Override
       
public
void
onReceive(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();
           
}
       
}
   
};
   

转载于:https://www.cnblogs.com/mfrbuaa/p/4197082.html

你可能感兴趣的文章
ActiveMQ与spring整合
查看>>
web服务器
查看>>
网卡流量检测.py
查看>>
poj1981 Circle and Points 单位圆覆盖问题
查看>>
POP的Stroke动画
查看>>
SQL语句在查询分析器中可以执行,代码中不能执行
查看>>
yii 1.x 添加 rules 验证url数组
查看>>
html+css 布局篇
查看>>
SQL优化
查看>>
用C语言操纵Mysql
查看>>
轻松学MVC4.0–6 MVC的执行流程
查看>>
redis集群如何清理前缀相同的key
查看>>
Python 集合(Set)、字典(Dictionary)
查看>>
获取元素
查看>>
proxy写监听方法,实现响应式
查看>>
第一阶段冲刺06
查看>>
十个免费的 Web 压力测试工具
查看>>
EOS生产区块:解析插件producer_plugin
查看>>
mysql重置密码
查看>>
jQuery轮 播的封装
查看>>