1

I'm a new Java programmer that works on Android SDK using Eclipse . I've created a browser using webview but the problem is that when I click download on any page it doesn't do anything while in the default browser in Android it downloads the file "Tested it on the same website" . I've searched for hours and I've seen some people using setDownloadListener but in my case when I click download it only opens a new page in the default browser . I don't know if that's important or not , but the file I'm downloading is MP3 .

Here's my code :

    public class MainActivity extends Activity {
   WebView web1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web1 = (WebView) findViewById(R.id.webView1);
        web1.setWebViewClient(new view());
        web1.loadUrl("http://site.org");
        web1.getSettings().setJavaScriptEnabled(true);
        web1.getSettings().setUseWideViewPort(true);
        web1.getSettings().setLoadWithOverviewMode(true);
          web1.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimetype,
                        long contentLength) {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });

I'm not sure if the setDownloadListener is put in the right place ? but anyway I also have a webclient to handle new links in the same browser :

public class view extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView v ,String url){
v.loadUrl(url);
return true;
F. Geraerts
  • 1,150
  • 17
  • 23
AlphaCode
  • 439
  • 1
  • 3
  • 17

0 Answers0