private class DownloadTextTask extends AsyncTask<String,Long,Long> {

        CharSequence contentText;
        Context context;
        CharSequence contentTitle;
        PendingIntent contentIntent;
        int ID = 1;
        long time;
        int icon;
        CharSequence tickerText;

        @Override
        protected Long doInBackground(String... urls) {
            InputStream inputStream = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse httpResponse = httpclient.execute(new HttpGet(urls[0]));
                inputStream = httpResponse.getEntity().getContent();
                byte[] buffer = IOUtils.toByteArray(inputStream);
                FileOutputStream fos = new FileOutputStream(MEDIA_PATH + "/fileName.mp3");
                fos.write(buffer);
                fos.flush();
                fos.close();
            } catch (Exception e) {
            }
            return (long) 100;
        }

        @Override
        protected void onPostExecute(Long result) {
            contentText = result + "% complete";
            contentTitle="Downloading Finished!";
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notificationManager.notify(ID, notification);
        }

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

         @Override
         public void onProgressUpdate(Long... progress) {
                super.onProgressUpdate(progress);
                contentText = progress[0] + "% complete";
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notificationManager.notify(ID, notification);
         }

           public void downloadNotification(){
                String ns = Context.NOTIFICATION_SERVICE;
                notificationManager = (NotificationManager) getSystemService(ns);

                icon = R.drawable.downicon;
                //the text that appears first on the status bar
                tickerText = "Downloading...";
                time = System.currentTimeMillis();

                notification = new Notification(icon, tickerText, time);

                context = getApplicationContext();
                //the bold font
                contentTitle = "Your download is in progress";
                //the text that needs to change
                contentText = "0% complete";
                Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
               // notificationIntent.setType("audio/*");
                contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notificationManager.notify(ID, notification);

            }
    }


我已经编写了这段代码来下载mp3文件,这里的问题是,它没有更新文件下载进度!我正在使用IOUtils类将InputStream转换为byte []。在这种情况下,我不知道如何发布进度!请帮助我。

最佳答案

我认为IOUtils.toByteArray(..)无法提供进度更新。如果文件很大,您可能根本不想将整个内容读入内存。您可以使用CountingInputStream跟踪读取的总字节数。

public long countContent(URL urls) {
  try {
     //...
     CountingInputStream counter = new CountingInputStream(httpResponse.getEntity().getContent());
     FileOutputStream os = new FileOutputStream(MEDIA_PATH + "/fileName.mp3");

     int read;
     byte[] buffer = new byte[1028];
     while ((read = counter.read(buffer)) != -1) {
        os.write(buffer, 0, read);
        publishProgress(counter.getByteCount()/size);
     }
     // ...
     return counter.getByteCount()/size;
  } catch (IOException ex) {
     throw new RuntimeException(ex);
  }
}

关于java - onProgressUpdate不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15934487/

10-10 02:14