2017-03-28 1 views
0

인터넷에서 mp3 파일을 다운로드하고 내부 저장소에 저장합니다. 다운로드 진행률은 ProgressDialog입니다. ProgressDialog은 진행률을 백분율로 표시합니다. 오른쪽에는 진행률이 1/100로 표시됩니다 .... MB 단위로 다운로드 한 파일의 현재 크기/MB 단위로 다운로드되는 파일의 총 크기를 표시하고 싶습니다.파일 다운로드 진행률 (단위 : MB)

다음 그림과 유사합니다. 회색 텍스트는 현재 표시된 텍스트입니다. 빨간색 아래에 텍스트처럼 표시되도록하고 싶습니다. XML 파일 내부해서 ProgressDialog 후 추가됩니다

public class DownloadSKHindi extends AsyncTask<String, Integer, String> { 

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

     progressDialog = new ProgressDialog(getActivity()); 
     progressDialog.setMessage("Downloading"); 
     progressDialog.setMax(100); 
     progressDialog.setCancelable(false); 
     progressDialog.setCanceledOnTouchOutside(false); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     int count = 0; 
     try { 
      url = new URL(skURL[10]); 

      connection = url.openConnection(); 
      connection.connect(); 

      int lengthOfFile = connection.getContentLength(); 

      InputStream input = new BufferedInputStream(url.openStream()); 

      OutputStream output = getActivity().openFileOutput(file.getPath(), Context.MODE_PRIVATE); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 

       publishProgress((int) (total * 100/lengthOfFile)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
     progressDialog.setProgress(values[0]); 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     progressDialog.dismiss(); 
    } 
} 
+0

. 먼저 파일 크기를 얻은 다음 바이트 단위로 각 백분율을 확인한 다음 MB로 변환하십시오. –

+0

@Devansh ** total ** 및 ** lengthOfFile ** –

+1

@GowthamanM의 크기를 찾는 방법은 위에서 볼 수 있듯이 'lengthOfFile'이 얻어집니다 내가 연결하고있는 파일의 길이를 얻음으로써. 그래서 당신이'AsyncTask'를 사용하여 다운로드하려고하는 파일이라면,'lengthOfFile'에'connection.getContentLength();'를 사용하십시오. 'total'은 얼마나 많은 파일이 다운로드되었는지 보여줍니다. 코드는 이미 그곳에 있습니다. – Devansh

답변

1

, 그리고 lengthOfFile 파일이 얼마나 많은 바이트의 크기입니다. 이 작업을 수행 할 수 있습니다

progressDialog.setProgressNumberFormat((bytes2String(total)) + "/" + (bytes2String(lengthOfFile))); 

그런 다음 함수 bytes2String 사용 (신용 coder에) 당신이 어떤 계산을 할 필요가

private static double SPACE_KB = 1024; 
private static double SPACE_MB = 1024 * SPACE_KB; 
private static double SPACE_GB = 1024 * SPACE_MB; 
private static double SPACE_TB = 1024 * SPACE_GB; 

public static String bytes2String(long sizeInBytes) { 

    NumberFormat nf = new DecimalFormat(); 
    nf.setMaximumFractionDigits(2); 

    try { 
     if (sizeInBytes < SPACE_KB) { 
      return nf.format(sizeInBytes) + " Byte(s)"; 
     } else if (sizeInBytes < SPACE_MB) { 
      return nf.format(sizeInBytes/SPACE_KB) + " KB"; 
     } else if (sizeInBytes < SPACE_GB) { 
      return nf.format(sizeInBytes/SPACE_MB) + " MB"; 
     } else if (sizeInBytes < SPACE_TB) { 
      return nf.format(sizeInBytes/SPACE_GB) + " GB"; 
     } else { 
      return nf.format(sizeInBytes/SPACE_TB) + " TB"; 
     } 
    } catch (Exception e) { 
     return sizeInBytes + " Byte(s)"; 
    } 

} 

Source

+0

** total ** 및 ** lengthOfFile **의 크기를 찾는 방법 –

1

사용 TextView :

enter image description here

여기에 현재 코드입니다. TextView tv = findViewById();

다음을 사용하여 publishProgress((int) (total * 100/lengthOfFile), total ,lengthOfFile); 새 데이터를 푸시하십시오.

그리고

tv 사용이 업데이트 : total 바이트 다운로드 얼마나 많은입니다 가정

@Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
     progressDialog.setProgress(values[0]); 
tv.setText((values[1]/(1024*1024)) + "" +(values[1]/(1024*1024))); 
    } 
+0

'TextView'를 가져올 필요는 없습니다. ProgressBar # setProgressNumberFormat을 사용할 수 있습니다. – JediBurrell

+0

@JediBurrell, 재미있는 아이디어,하지만 그는 붉은 색 거대한 텍스트를 원합니다. – Vyacheslav

+0

저는 그것이 단지 예일 뿐이라고 확신합니다. 나는 틀릴지도 모른다. – JediBurrell

관련 문제