如何解决下载管理器冻结应用程序并退出如果我销毁应用程序
我正在使用android下载管理器下载图像(大约400到1000张图像),首先要下载外部存储(临时文件夹)中的所有图像,然后将这些文件复制到内部存储文件夹中(因为DownloadManger服务没有写入应用程序专用文件夹的权限)。
案例1:
单击下载按钮后,应用程序冻结,但没有响应,但通知文件开始下载。
案例2:
在下载过程中退出/销毁应用程序时,一段时间后挂起的文件停止下载。
我正在循环使用下载管理器,循环代码如下所示
for (int i=0; i<=imageUrls.length-1; i++){
downloadmngr(imageUrls[i],i);
}
还有带有下载管理器代码的下载图像
public void downloadmngr(String urlimg,int imgname){
String downloadFileName = urlimg.substring(urlimg.length() - 4);
list.clear();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlimg));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(imgname + downloadFileName);
request.setDescription(imgname + downloadFileName);
request.setVisibleInDownloadsUi(true);request.setDestinationInExternalPublicDir(Environment.getDataDirectory().getPath(),"/"+BuildConfig.S_ampledata+"/" +novelname+ "/"+ imgname + downloadFileName);
refid = downloadManager.enqueue(request);
Log.e("OUTNM","" + refid);
list.add(refid);
}
还有广泛的广播接收方代码
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt,Intent intent) {
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
Log.e("IN","" + referenceId);
list.remove(referenceId);
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID,0);
/*Here I am deleting every single file from TEMP folder after file download and copy to PRIVATE folder Completes */
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
/*Here First I Query the downloaded file*/
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(referenceId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri a = Uri.parse(uriString);
d = new File(a.getPath());
/*Here I am Copying the file to private folder*/
try {
int bytesum = 0;
int byteread = 0;
InputStream inStream = new FileInputStream(d);
FileOutputStream fs = new FileOutputStream(MainActivity.this.getFilesDir() + "/"+BuildConfig.S_privatefolder+"/"+novelname+"/"+"a"+d.getName());
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer,byteread);
}
inStream.close();
fs.close();
d.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
}
}).start();
}
/*In Bellow Code I Emptying / Deleting the Public / Temp folder after all files download complete*/
if (list.isEmpty())
{
Log.e("INSIDE","" + referenceId);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("GadgetSaint")
.setContentText("All Download completed");
Toast.makeText(getBaseContext(),novelname+" Downloading Complete",Toast.LENGTH_SHORT).show();
if (publickdir.exists()){
File[] files2 = publickdir.listFiles();
if(files2.length >0)
{
for(int pd=0; pd<files2.length; pd++) {
if(files2[pd].isFile()) {
files2[pd].delete();
}
}
publickdir.delete();
}else {
publickdir.delete();
}
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(455,mBuilder.build());
}
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。