Gle*_*tia 9 java mp3 android download webview
我想通过使用webview从网站下载文件(例如.mp3),但问题是每当我点击链接时,它将打开浏览器(默认一个),它在关闭之前出现一秒.并且没有下载任何文件.
这是我的代码,
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
WebView webview;
Button bt_search;
TextView txt_search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webView);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
txt_search = (TextView) findViewById(R.id.song);
webview.loadUrl("http://www.google.com");
bt_search = (Button) findViewById(R.id.findit);
bt_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String keyword = txt_search.getText().toString().trim();
if (!keyword.equals("")) {
webview.loadUrl("MP3 Sites" + keyword + ".html");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
Kry*_*lez 10
实现WebViewClient以与WebView一起使用.在其中,覆盖shouldOverrideUrlLoading方法,您应该检查它是否是mp3文件,然后将该URL传递给DownloadManager或您用于实际下载文件的任何内容.这是一个粗略的想法:
// This will handle downloading. It requires Gingerbread, though
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// This is where downloaded files will be written, using the package name isn't required
// but it's a good way to communicate who owns the directory
final File destinationDir = new File (Environment.getExternalStorageDirectory(), getPackageName());
if (!destinationDir.exists()) {
destinationDir.mkdir(); // Don't forget to make the directory if it's not there
}
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
boolean shouldOverride = false;
// We only want to handle requests for mp3 files, everything else the webview
// can handle normally
if (url.endsWith(".mp3")) {
shouldOverride = true;
Uri source = Uri.parse(url);
// Make a new request pointing to the mp3 url
DownloadManager.Request request = new DownloadManager.Request(source);
// Use the same file name for the destination
File destinationFile = new File (destinationDir, source.getLastPathSegment());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
}
return shouldOverride;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20199 次 |
| 最近记录: |