2017-09-27 87 views
0
  1. 代码下载文件:setProgress方法不更新进度条,当我的文件被下载

    Dialog dialog; 
    int totalSize = 0; 
    ProgressBar mProgressBar; 
    int downloadedSize = 0; 
    
    public void downloadFile(){ 
        try { 
         URL url = new URL("http://.../my_file.mp3"); 
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    
         urlConnection.setRequestMethod("GET"); 
         urlConnection.setDoOutput(true); 
    
         urlConnection.connect(); 
    
         File SDCardRoot = Environment.getExternalStorageDirectory(); 
    
         File dir = new File(SDCardRoot.getAbsolutePath(), "/dossier1/dossier2"; 
         dir.mkdirs(); 
    
         File file = new File(dir, "my_file.mp3"); 
    
         FileOutputStream fileOutput = new FileOutputStream(file); 
    
         InputStream inputStream = urlConnection.getInputStream(); 
    
         totalSize = urlConnection.getContentLength(); 
    
         runOnUiThread(new Runnable() { 
          public void run() { 
           mProgressBar.setMax(totalSize); 
          } 
         }); 
    
         byte[] buffer = new byte[1024]; 
         int bufferLength = 0; 
    
         while ((bufferLength = inputStream.read(buffer)) > 0) { 
          fileOutput.write(buffer, 0, bufferLength); 
          downloadedSize += bufferLength; 
    
          runOnUiThread(new Runnable() { 
           public void run() { 
            mProgressBar.setProgress(downloadedSize); 
           } 
          }); 
         } 
    
         fileOutput.close(); 
    
         runOnUiThread(new Runnable() { 
          public void run() { 
           //mProgressBar.dismiss(); 
          } 
         }); 
    
        } catch (final MalformedURLException e) { 
         showError("Error : MalformedURLException " + e); 
         e.printStackTrace(); 
        } catch (final IOException e) { 
         showError("Error : IOException " + e); 
         e.printStackTrace(); 
        } 
        catch (final Exception e) { 
         showError("Error : Please check your internet connection " + e); 
        } 
    } 
    
  2. showProgress方法:

    public void showProgress(){ 
    dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.myprogressdialog); 
    dialog.setTitle("Download Progress"); 
    
    TextView text = (TextView) dialog.findViewById(R.id.tv1); 
    text.setText("Downloading..."); 
    cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv); 
    cur_val.setText("Starting download..."); 
    dialog.show(); 
    
    mProgressBar = (ProgressBar)dialog.findViewById(R.id.progress_bar); 
    mProgressBar.setProgress(0); 
    mProgressBar.setProgressDrawable(context.getResources().getDrawable(R.drawable.blue_progress)); 
    } 
    
  3. 布局为mProgressDialog( myprogressdialog.xml中的res/layout):

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout_root" 
    android:orientation="vertical" 
    android:padding="10dp"> 
    
    <TextView 
    android:id="@+id/tv1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#FFF" 
    android:text="hello" 
    android:textStyle="bold" 
    /> 
    
    <ProgressBar 
    android:id="@+id/progress_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:progress="0" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="10dp" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:maxHeight="10dip" 
    android:minHeight="10dip" 
    /> 
    
    <TextView 
    android:id="@+id/cur_pg_tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#0F0" 
    android:text="hello" 
    android:layout_marginTop="5dp" 
    android:textStyle="bold|italic" /> 
    
    </LinearLayout> 
    
  4. XML进行(在res blue_progress.xml /绘制)给人一种蓝色的mProgressBar

    <?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    
    <item android:id="@android:id/background"> 
    <shape> 
        <corners android:radius="5dip" /> 
        <gradient android:startColor="#ff9d9e9d" 
         android:centerColor="#ff5a5d5a" 
         android:centerY="0.75" 
         android:endColor="#ff747674" 
         android:angle="270" 
         /> 
    </shape> 
    </item> 
    <item android:id="@android:id/progress"> 
    <clip> 
        <shape> 
         <corners android:radius="5dip" /> 
         <gradient android:startColor="@android:color/holo_blue_bright" 
          android:centerColor="@android:color/holo_blue_dark" 
          android:centerY="0.75" 
          android:endColor="@android:color/holo_blue_light" 
          android:angle="270" 
          /> 
        </shape> 
    </clip> 
    </item> 
    </layer-list> 
    

setProgress方法下载我的文件时不更新进度! 你能告诉我任何解决方案吗?

回答

0

https://developer.android.com/reference/android/os/AsyncTask.html

我建议你上的AsyncTask阅读了,因为里面还有一些非常好的工具,可以帮助你异步更新UI线程。
异步任务允许您在后台线程上运行任务,并在完成(或正在进行)时为UI提供更新。通常这可以让你跳过制作UI Runnable - 但这取决于你的情况。

无论如何, 您想重写的方法是在AsyncTask中的onProgressUpdate。此外,它可能会为您在未来尝试完成此项工作节省一些麻烦。

祝你好运!希望有所帮助。

+0

您是否发现需要解决问题? – DWndrer