下载前获取文件名



我想用我自己的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;
        }

使用如下代码(可能需要更少的代码):

  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");
        }
      }

相关内容

  • 没有找到相关文章

最新更新