下载前获取文件名

时间:2014-08-09 13:48:32

标签: android download-manager

我想使用自己的webview下载一些文件,但是当我使用下载管理器时,我无法在下载之前获取该文件的默认名称。

我尝试了这个,但它不起作用

String fileName = url.substring(url.lastIndexOf('/') + 1);

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName );

有什么想法吗?

你需要什么:

@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url2) {


            if(url2.startsWith("http://www.youtube-mp3.org/get?ab=")){


            if(isDownloadManagerAvailable(MainActivity.this)){


                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url2));


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }


                request.setMimeType("audio/MP3");
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);


//Get file name as string


                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, -----);//put it here


                manager.enqueue(request);




            }
            }
            else{
                view.loadUrl(url2);

                }
            return false;
        }

1 个答案:

答案 0 :(得分:5)

使用类似的东西(可能需要更少的代码):

  public class MainActivity extends ActionBarActivity
      {
      @Override
      protected void onCreate(final Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView webView=(WebView)findViewById(R.id.webView1);
        final WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webView.setWebViewClient(new WebViewClient()
          {
            @Override
            public boolean shouldOverrideUrlLoading(final WebView view,final String url)
              {
              return false;
              }
          });
        webView.setDownloadListener(new DownloadListener()
          {
            @Override
            public void onDownloadStart(final String url,final String userAgent,final String contentDisposition,final String mimetype,final long contentLength)
              {
              final String fileName=URLUtil.guessFileName(url,contentDisposition,mimetype);
              android.util.Log.d("Applog","fileName:"+fileName);
              // start downloading:
            //  final Intent i=new Intent(Intent.ACTION_VIEW);
            //  i.setData(Uri.parse(url));
           //   startActivity(i);


  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }


            request.setMimeType("audio/MP3");
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);





            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);


            manager.enqueue(request); 
              }
          });
        webView.setWebChromeClient(new WebChromeClient()
          {});
        webView.loadUrl("http://www.youtube-mp3.org/get?ab=128&video_id=KMU0tzLwhbE&h=1c1f479c948bb9cfb495605c7214cb49&r=1407775588334.1531971212");
        }
      }