我使用DownloadManager下载文件,并从DownloadManager.COLUMN_LOCAL_URI中取名。下载同一个文件超过11次。 11次后开始怀疑奇怪的名字。例如:
的text.txt
文本1.txt的
文本2.txt
文本3.txt
文本4.txt
文本5.txt
文本6.txt
文本7.txt
文本8.txt
文本9.txt
文本10.txt
文本26.txt
文本14.txt
可能是什么问题?
提前谢谢
编辑:
带上我的代码
public static long downloadFile(Context ctx, String url, String title, String description, String filename, String mimetype, BroadcastReceiver onDownloadComplete) {
DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
long res = dm.enqueue(new DownloadManager.Request(Uri.parse(url)).setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI).setAllowedOverRoaming(false).setTitle(title).setMimeType(mimetype).setDescription(description).setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename));
ctx.registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Toster.showGreenToast(ctx, ctx.getString(R.string.download_started, filename));
return res;
}
public static String getDownloadCompletedFileName(Context ctx, Intent intent) {
String res = "";
try {
Bundle extras = intent.getExtras();
DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = dm.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
res = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
// get other required data by changing the constant passed to getColumnIndex
}
}
c.close();
} catch (Exception e) {}
return res;
}
答案 0 :(得分:0)
也许您应该在创建DownloadManager.Request时使用setDestinationInExternalFilesDir()
,setDestinationInExternalPublicDir()
或setDestinationUri()
来设置文件名和目录。
答案 1 :(得分:0)
请参阅http://developer.android.com/reference/android/app/DownloadManager.html#ERROR_FILE_ALREADY_EXISTS
正如它所说:下载管理器不会覆盖现有文件
正如我所看到的,它只是在文件名的末尾添加“-n”而不是抛出异常。