【问题标题】:how to download file in android download service如何在android下载服务中下载文件
【发布时间】:2013-06-10 07:30:07
【问题描述】:

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

当我把它放到电脑和安卓浏览器上时,它让我强制下载 如何在没有浏览器的情况下将其下载到我的 android 应用程序中。 我尝试使用本教程进行下载how can i download audio file from server by 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();
            }

        }

    });

【问题讨论】:

    标签: android service download


    【解决方案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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      相关资源
      最近更新 更多