下载MP3文件在Android的问题 [英] Downloading mp3 files problem in Android

查看:110
本文介绍了下载MP3文件在Android的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,其允许下载音频文件并将其存储在自己的SD卡的用户。下面的code的实际运行状态,但问题是,每次我打的下载按钮,整个应用程序冻结,直到下载结束。我该如何解决这个问题,以便它不会冻结每当我打的下载按钮?请帮我,我一直在挣扎了好几天,试图找出这个TE解决方案,我真的AP preciate你有很大的帮助。先谢谢了。

尝试{

 网​​址URL =新的URL(HTTP://www.yourfile.mp3);
            HttpURLConnection的C =(HttpURLConnection类)url.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真正的);
            c.connect();

            字符串PATH = Environment.getExternalStorageDirectory()
                    +/下载/;
            Log.v(,路径:+路径);
            档案文件=新的文件(路径);
            file.mkdirs();

             字符串文件名=yourfilename.mp3;

            文件OUTPUTFILE =新的文件(文件,文件名);
            FileOutputStream中FOS =新的FileOutputStream(OUTPUTFILE);

            InputStream的是= c.getInputStream();

            byte []的缓冲区=新的字节[1024];
            INT LEN1 = 0;
            而((LEN1 = is.​​read(缓冲液))!=  -  1){
                fos.write(缓冲液,0,LEN1);
            }
            fos.close();
            is.close();

            }赶上(IOException异常E){
                   e.printStackTrace();
            }

        }
 

解决方案

由于您的下载方式下载作品的文件,它会创建一个即时pression喜欢的应用程序已经冻结。但事实是你的方法是执行在后面。因此,为了避免应用程序冻结你可以显示一个ProgressDialog直到下载方法是下载并使它运行起来后,你可以取消ProgressDialog。

所以,你必须使用一个单独的线程进行下载。下面是说明如何下载并在平均时间显示的progressDialog的链接。

http://www.bogotobogo.com/Android/android23HTTP.html

I created an app which allows the user to download audio file and store it in their SD card. The code below is actually working, but the problem is every time I hit the download button, the whole app freezes until the download is over. How can I solve this issue so that it wont freeze whenever I hit the download button? Please help me, I have been struggling for days trying to figure out te solution for this, I would really appreciate your help a lot. Thanks in advance.

try {

            URL url = new URL("http://www.yourfile.mp3");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

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

             String fileName = "yourfilename.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) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

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

        }

解决方案

Since your download method works downloading your file, it will create an impression like the app has freezed. But the truth is your method is executing in the back. So to avoid app freeze you can show an ProgressDialog til the download method is downloading and after it gets executed you can cancel the ProgressDialog.

So you have to use a separate thread to download. Here is the link which explains you how to download and show an progressDialog in the mean time.

http://www.bogotobogo.com/Android/android23HTTP.html

这篇关于下载MP3文件在Android的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆