Reputation: 366
I use DownloadManager for downloading files and take their names from DownloadManager.COLUMN_LOCAL_URI. Downloading the same file more than 11 times. After 11 times start to wonder strange names. Example:
text.txt
text-1.txt
text-2.txt
text-3.txt
text-4.txt
text-5.txt
text-6.txt
text-7.txt
text-8.txt
text-9.txt
text-10.txt
text-26.txt
text-14.txt
What could be the problem?
Thank you in advance
EDIT:
Bring my code
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;
}
Upvotes: 1
Views: 1826
Reputation: 1868
See http://developer.android.com/reference/android/app/DownloadManager.html#ERROR_FILE_ALREADY_EXISTS
As it says there: the download manager will not overwrite an existing file
As I see it, instead of throwing an exception it will just append a "-n" to the end of the file name.
Upvotes: 1
Reputation: 36
Maybe you should use the setDestinationInExternalFilesDir()
, setDestinationInExternalPublicDir()
or setDestinationUri()
when create the DownloadManager.Request to set the filename and directory.
Upvotes: 0