簡體   English   中英

如何獲取從Android中的下載管理器下載的文件的網絡uri

[英]How to get the network uri of a file being downloaded from the Download Manager in Android

我正在編寫一個應用程序,其中我要檢測下載是否已開始並檢索正在下載的文件的URI,然后從下載管理器中取消下載。 我這樣做是為了可以將此URI發送到其他地方。

問題是我可以通過查詢下載管理器來檢測何時開始下載,但是下載管理器中是否存在方法或常量變量,我也可以從中獲取正在下載文件的URL

好的,很奇怪地回答了您自己的問題,但是我終於想出了解決方法。 android.app中有一個DownloadManager類,該類存儲所有已啟動的http下載及其狀態的列表。 可以根據下載是否為“正在運行”,“正在掛起”,“已暫停”等來過濾掉這些文件。

可以將該列表讀入游標,結果的列之一是“ COLUMN_URI”,這是從中下載文件的URL。 我在其中使用過的示例代碼如下:

public void readDownloadManager() {
                DownloadManager.Query query = null;
                DownloadManager downloadManager = null;
                Cursor c = null;
                try {
                    query = new DownloadManager.Query();
                    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);

                    //Just for testing I initiated my own download from this url. When an http
                    // reuest for this url is made, since download is taking place, it gets saved in
                    // the download manager.
                    Request request = new Request(Uri.parse("http://ocw.mit.edu/courses" +
                            "/aeronautics-and-astronautics/16-100-aerodynamics-fall-2005" +
                            "/lecture-notes/16100lectre1_kvm.pdf"));
                    downloadManager.enqueue(request);
                    query.setFilterByStatus(DownloadManager.STATUS_PENDING);
                    c = downloadManager.query(query);

                    if(true){
                        int statusColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int urlColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_URI);
                        long downloadProcessIdColumnNo = c.getColumnIndex(DownloadManager.COLUMN_ID);
                        Log.d("Column Count", ((Integer)c.getCount()).toString());
                        if(c.getCount() > 0){
                            String url="";
                            c.moveToLast();
                            if(c.isLast()){
                                url = c.getString(urlColumnIndex);
                                downloadManager.remove(downloadProcessIdColumnNo);
                                Log.d("Count after remove", ((Integer)c.getCount()).toString());
                            }
                            Log.d("After", "Stopped Working");

                            //Here I am sending the url to another activity, where I can work with it.
                            Intent intent = new Intent(EasyUploadMainMenu.this, EasyUploadActivity.class);
                            Bundle b = new Bundle();
                            b.putString("url", url);
                            intent.putExtras(b);
                            startActivity(intent);
                            Log.d("url:", url);
                        }
                    }

                } catch (NullPointerException ex) {
                    ex.printStackTrace();
                }
            }

暫無
暫無

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

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