簡體   English   中英

Android DownloadManager獲取文件名

[英]Android DownloadManager get filename

在我的應用程序中,您可以下載一些文件 我使用Android DownloadManager類進行下載。 下載完成后,它會顯示一條消息,說明該文件已下載。 問題是,可能同時有2,3或4次下載。 我的BroadcastReceiver代碼如下所示:

receiver_complete = new BroadcastReceiver(){
         @Override
          public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
                    Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.download_finished, "Here should be the name", Toast.LENGTH_SHORT).show();
                }
         }
     };

如何獲取完成下載的當前文件名?

非常感謝你。

我想你想在if塊里放這樣的東西。 YOUR_DM替換為您的DownloadManager實例。

Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);

if (c.moveToFirst()) {
    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        // process download
        title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
        // get other required data by changing the constant passed to getColumnIndex
    }
}

Ian Shannon對他的答案是完全正確的,但我有一些改進:

  • 請記住在使用后關閉該Cursor ,避免“光標泄漏”。 Cursor消耗大量資源,必須盡快釋放。

  • 如果您為下載添加了一些標題,例如:

     DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle("Some title"); 

    DownloadManager.COLUMN_TITLE給出的值將是"Some title"而不是文件名。 所以我建議這樣做:

     String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); title = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() ); 

    COLUMN_LOCAL_FILENAME返回整個路徑( /storage/sdcard0/Android/data/.../filename.ext ),但是使用此代碼,我們只會獲取文件名。

最終代碼:

Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);

if (c.moveToFirst()) {
    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
        filename = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
    }
}
c.close();

編輯:用您的DownloadManager實例替換YOUR_DM。

我想你要找的是DownloadManager.COLUMN_TITLE

以下是Android文檔的鏈接: http//developer.android.com/reference/android/app/DownloadManager.html#COLUMN_TITLE

這是一個教程,不僅僅是解釋標題。 它用於從URL下載圖像,然后將其顯示在應用程序中。 總的來說,我認為強大有用。

http://wptrafficanalyzer.in/blog/downloading-an-image-from-an-http-url-using-downloadmanager-and-displaying-in-imageview-by-dynamically-registered-broadcastreceiver/

你試試這個代碼來獲取文件名

DownloadManager.COLUMN_LOCAL_FILENAME

我不確定

有一種更簡單的方法來檢索下載的文件URI,這與getUriForDownloadedFile

download_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        long file_id = b.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
        Uri uri = dm.getUriForDownloadedFile(downloaded_file_id);

    }
}

我在這里找到了這個方法,詳細描述了如何使用BroadcastReceiver來處理DOWNLOAD_COMPLETE事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM