[您好,我是Android开发的新手,正在webview
上工作,我已经创建了网络音乐播放器,可以在其中播放和下载音乐。 播放器功能在Web浏览器上运行良好,但在android设备上,下载功能不起作用。
以下代码用于webview
-
WebView webView = findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
if (!b1) {
webView.setVisibility(View.GONE);
} else {
webView.setVisibility(View.VISIBLE);
webView.loadUrl("http://music.in/android_music/music.html");
}
播放器设计如下。突出显示的下载图标在android webview
中不起作用。
我要将文件下载并存储在Music
文件夹中。请帮忙。在此先感谢您的帮助...!
您可以尝试这种方法
创建这样的Web界面
public class JSInterface {
Context context;
JSInterface (Context context) {
this.context= context;
}
@JavascriptInterface
public void downloadFile(String url) {
// Do your downloading task
}
}
然后像这样在html内定义方法
<input type="button" value="Say hello" onClick="downloadFilefromUrl('url')" />
<script type="text/javascript">
function downloadFilefromUrl(url) {
if(typeof Android !== "undefined" && Android !== null) {
android.downloadFile(url);
} else {
alert("error");
}
}
</script>