-1

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

当我将它放置到 pc 和 android 浏览器时,它让我强制下载如何让它在没有浏览器的情况下在我的 android 应用程序中下载。我尝试使用本教程进行下载,如何通过 url 从服务器下载音频文件。 但它不起作用。任何人请帮忙


谢谢 Kristijana Draca 认为它正在工作,但它保存在模拟器中的地方是我的代码 public class Main extends 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();
            }

        }

    });
4

1 回答 1

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);
    }

}
于 2013-06-10T07:37:52.773 回答