Mp3下载代码在Android应用程序中不起作用

时间:2017-11-25 05:48:37

标签: android

我正在添加此代码以从我的网站下载mp3文件,但是当我点击下载时,应用程序崩溃了。那该怎么办?

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // handle different requests for different type of files
    // this example handles downloads requests for .apk and .mp3 files
    // everything else the webview can handle normally
    if (url.endsWith(".mp3")) {
        Uri source = Uri.parse(url);
        // Make a new request pointing to the .apk url
        DownloadManager.Request request = new DownloadManager.Request(source);
        // appears the same in Notification bar while downloading
        request.setDescription("Check Folder after Downloading Done!");
        request.setTitle("Downloading From Marathimaza");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        // save the file in the "Downloads" folder of SDCARD
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Downloaded");
        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }

    else view.loadUrl(url);
    return true;
  }
}

0 个答案:

没有答案