如何在android下载服务中下载文件

时间:2013-06-10 07:30:08

标签: android service download

我有这个网址http://translate.google.com/translate_tts?ie=UTF-8&q=hi&tl=en&total=1&idx=0&textlen=2

当我把它放到PC和Android浏览器时,它让我有力下载 如何在没有浏览器的情况下在我的Android应用程序中下载它。 我尝试使用本教程进行下载how can i download audio file from server by url 但它不起作用。 有人请帮忙


谢谢Kristijana Draca认为它正在工作,但它在模拟器中保存的地方是我的代码公共类Main扩展Activity {

EditText inputtext;
Button listen;
Button shareButton;
TextView tv;
ProgressBar proBar; 
//ProgressDialog progress;
MediaPlayer player;
public Boolean isPlaying=true;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            downloadContent();
        }



        private void downloadContent() {
                DownloadFile downloadFile = new DownloadFile();
                downloadFile.execute("http://translate.google.com/translate_a/t?client=t&source=baf&sl=ar&tl=en&hl=en&q=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7&sc=1 ");
        }

        // usually, subclasses of AsyncTask are declared inside the activity class.
        // that way, you can easily modify the UI thread from here
        class DownloadFile extends AsyncTask<String, Integer, String> {
            @Override
            protected String doInBackground(String... sUrl) {
                try {
                    URL url = new URL(sUrl[0]);
                    URLConnection connection = url.openConnection();
                    connection.connect();

                    int fileLength = connection.getContentLength();

                    InputStream input = new BufferedInputStream(
                            connection.getInputStream());
                    // Create db
                    OutputStream output = new FileOutputStream(
                            Environment.getDataDirectory() + "/data/"
                                    + "com.jony.com" + "/file.mp3");

                    byte data[] = new byte[1024];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                        publishProgress((int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();

                } catch (Exception e) {
                }
                return null;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onProgressUpdate(Integer... progress) {
                super.onProgressUpdate(progress);
                Toast.makeText(getApplicationContext(), "download complete", 1000).show();
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                Toast.makeText(getApplicationContext(), "download complete", 1000).show();
            }

        }

    });

1 个答案:

答案 0 :(得分:1)

您可以使用AsyncTask下载任何文件。

downloadContent();

private void downloadContent() {
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute("http://somehost.com/file.mp3");
}

// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();

            int fileLength = connection.getContentLength();

            InputStream input = new BufferedInputStream(
                    connection.getInputStream());
            // Create db
            OutputStream output = new FileOutputStream(
                    Environment.getDataDirectory() + "/data/"
                            + PACKAGE_NAME + "/file.mp3");

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}