1

ファイルのダウンロードには 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

text-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;
    }
4

2 に答える 2

1

http://developer.android.com/reference/android/app/DownloadManager.html#ERROR_FILE_ALREADY_EXISTSを参照してください。

そこにあるように、ダウンロードマネージャーは既存のファイルを上書きしません

私が見たように、例外をスローする代わりに、ファイル名の末尾に「-n」を追加するだけです。

于 2014-10-03T12:26:50.617 に答える
0

ファイル名とディレクトリを設定するためにDownloadManager.Requestを作成するときにsetDestinationInExternalFilesDir()setDestinationInExternalPublicDir()またはを使用する必要があるかもしれません。setDestinationUri()

于 2014-01-14T21:37:46.127 に答える