【问题标题】:Using Android DownloadManager,how do i get file name?使用 Android DownloadManager,我如何获取文件名?
【发布时间】:2016-02-26 11:25:45
【问题描述】:

我正在使用 DownloadManager 类来管理下载,我正在使用这个

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,subPath);

目前我正在将 subPath 设置为某个文件名,但是如何从 url 获取文件名?

我不想使用游标,而是想通过某种方式从 uri 中提取文件名。

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    好吧,如果没有光标,那么试试这个:

    class GetFileName extends AsyncTask<String, Integer, String>
    {
    protected String doInBackground(String... urls)
    {
        URL url;
        String filename = null;
        try {
            url = new URL(urls[0]);
            String cookie = CookieManager.getInstance().getCookie(urls[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Cookie", cookie);
            con.setRequestMethod("HEAD");
            con.setInstanceFollowRedirects(false);
            con.connect();
    
            String content = con.getHeaderField("Content-Disposition");
            String contentSplit[] = content.split("filename=");
            filename = contentSplit[1].replace("filename=", "").replace("\"", "").trim();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
        }
        return filename;
    }
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String result) {
    
    }
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      DownloadManager.COLUMN_TITLE
      

      请看Android DownloadManager get filename

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2012-10-30
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-22
        相关资源
        最近更新 更多