-1

i have this url http://translate.google.com/translate_tts?ie=UTF-8&q=hi&tl=en&total=1&idx=0&textlen=2

when i place it to pc and android browser it makes me force to download how can i make it download in my android application without browser. i tried to make to download using this tutorial how can i download audio file from server by url .but it did not work. anyone please help


Thank you Kristijana Draca think it is working but where it save in emulator here is my code 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();
            }

        }

    });
Community
  • 1
  • 1
shakac
  • 379
  • 4
  • 15

1 Answers1

1

You can download any file using 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);
    }

}
Kristijan Drača
  • 614
  • 4
  • 16