-1

私はこの URL 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 さんありがとうございます

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 に答える