Android-通过URL命名问题将mp3下载到SDCARD

发布于 2024-11-26 11:34:31 字数 4244 浏览 2 评论 0原文

我目前有一个带有各种 mp3 链接的 webview 。如果用户按下这些链接之一,将弹出一个 alertDialog,他们可以选择是否收听或下载该文件。虽然我的下载部分按预期工作(通过asynctask),但我当前已将其设置为指定 SDCARD 上的 mp3 文件将被调用的名称。我希望曲目的名称就是 mp3 文件的名称。关于我如何做到这一点有什么想法吗?谢谢。

这是我的代码的一部分:

//so you can click on links in app and not open the actual browser. will stay in app
    private class HelloWebViewClient extends WebViewClient{     
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url){
            view.loadUrl(url);
            view.getSettings().getAllowFileAccess();
            view.getSettings().setJavaScriptEnabled(true);
            //load the dropbox files so people can listen to the track
            if(url.endsWith(".mp3")){
                progressWebView.dismiss();
                progressWebView.cancel();
                blogDialog.setButton("Listen", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "audio/*");
                        view.getContext().startActivity(intent);

                    }
                });
                blogDialog.setButton2("Download", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sdrUrl = url.toString();
                        new DownloadFile().execute();   


                    }   

                });
                blogDialog.show();

            }else{
                return super.shouldOverrideUrlLoading(view, url);
            }
            return true;
        }
    }

    //to handle the back button 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if((keyCode == KeyEvent.KEYCODE_BACK) && sdrWebView.canGoBack()){
            sdrWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onPause(){
        super.onPause();
    }

    /*create the pop up menu so you can reload*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.refreshsetting: sdrWebView.loadUrl("http://www.stopdroprave.com");
        break;
        }
        return true;
    }

    private class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... url) {
            try {
                URL url2 = new URL(sdrUrl);
                HttpURLConnection c = (HttpURLConnection) url2.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();

                int lengthOfFile = c.getContentLength();

                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                Log.v("", "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                 String fileName = "testSDRtrack.mp3";

                File outputFile = new File(file, fileName);
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    publishProgress((int)(len1*100/lengthOfFile));
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

                }catch (IOException e) {
                       e.printStackTrace();
                }


            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            super.onProgressUpdate(values);
        }



}
}

I currently have a webview with various mp3 links. If the user presses on one of these links, an alertDialog will pop up and they can choose whether to listen or download the file. While my download portion works intended (via an asynctask), I have it currently set up where I specify the name which the mp3 file on the SDCARD will be called. I would like to have it so that the name of the track is the name of the mp3 file. Any ideas on how I could do that? Thanks.

Here is portion of my code:

//so you can click on links in app and not open the actual browser. will stay in app
    private class HelloWebViewClient extends WebViewClient{     
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, final String url){
            view.loadUrl(url);
            view.getSettings().getAllowFileAccess();
            view.getSettings().setJavaScriptEnabled(true);
            //load the dropbox files so people can listen to the track
            if(url.endsWith(".mp3")){
                progressWebView.dismiss();
                progressWebView.cancel();
                blogDialog.setButton("Listen", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "audio/*");
                        view.getContext().startActivity(intent);

                    }
                });
                blogDialog.setButton2("Download", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sdrUrl = url.toString();
                        new DownloadFile().execute();   


                    }   

                });
                blogDialog.show();

            }else{
                return super.shouldOverrideUrlLoading(view, url);
            }
            return true;
        }
    }

    //to handle the back button 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if((keyCode == KeyEvent.KEYCODE_BACK) && sdrWebView.canGoBack()){
            sdrWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onPause(){
        super.onPause();
    }

    /*create the pop up menu so you can reload*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.refreshsetting: sdrWebView.loadUrl("http://www.stopdroprave.com");
        break;
        }
        return true;
    }

    private class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... url) {
            try {
                URL url2 = new URL(sdrUrl);
                HttpURLConnection c = (HttpURLConnection) url2.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();

                int lengthOfFile = c.getContentLength();

                String PATH = Environment.getExternalStorageDirectory()
                        + "/download/";
                Log.v("", "PATH: " + PATH);
                File file = new File(PATH);
                file.mkdirs();

                 String fileName = "testSDRtrack.mp3";

                File outputFile = new File(file, fileName);
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    publishProgress((int)(len1*100/lengthOfFile));
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

                }catch (IOException e) {
                       e.printStackTrace();
                }


            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            super.onProgressUpdate(values);
        }



}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-12-03 11:34:31

我基本上分割了 url 并得到了我想要的部分,然后像

Android- split URL string

I essentially split the url and got the portion that I wanted then saved it like that

Android- split URL string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文