Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android webview上下载.mp3文件_Android_Webview - Fatal编程技术网

在Android webview上下载.mp3文件

在Android webview上下载.mp3文件,android,webview,Android,Webview,我需要帮助 我有以下活动: import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class WebViewActivity extends Activity { private WebView webView; public void onCreate(Bundle savedInstanceState) {

我需要帮助

我有以下活动:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {
        private WebView webView;
        public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.webview);

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadUrl("file:///android_asset/index.html");
        }

}

如果url=.mp3,如何下载文件?

最好使用异步任务

protected String doInBackground(String... arg0) {
            int count;
            try {
                System.out.println("File Path in doBackground: "+GlobalVariable.getStrPath());
                URL url = new URL(GlobalVariable.getStr());
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                int lenghtOfFile = c.getContentLength();
                String PATH = Environment.getExternalStorageDirectory() + "/foldername/";
                Log.v("appname", "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                File outputFile = new File(file, fileName);

                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                long total = 0;
                System.out.println("filesize usibng buffer: "+is.read(buffer));
                while ((count = is.read(buffer)) != -1) {
                    total += count;
//                  publishProgress("" + (long) ((total * 100) / count));
                //  publishProgress("" + (long) ((total) / count));
                    publishProgress("" + (long) ((total) / count));
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();

            } catch (IOException e) {
                Log.d("myappname", "Error: " + e);
            }
            return null;
        }

让系统负责您的mp3文件下载。任何需要下载的东西都会让用户很难找到文件

wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(!url.contains(".mp3")) {
                    wv.loadUrl(url);
                }
                else{
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
                return true;
            }
        });

你想说什么?你想要什么?