2012-12-09 4 views

답변

1

이 트릭을 할해야합니다. 이 파일을 sdcard/Download/products.txt에 다운로드합니다. 아래에서 변경할 수 있습니다.

try { 
    URL url = new URL("http://base.google.com/base/products.txt"); 
    URLConnection conexion = url.openConnection(); 
    conexion.connect(); 
    int lenghtOfFile = conexion.getContentLength(); 
    InputStream is = url.openStream(); 
    File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download"); 
    if (!testDirectory.exists()) { 
     testDirectory.mkdir(); 
    } 
    FileOutputStream fos = new FileOutputStream(testDirectory + "/products.txt"); 
    byte data[] = new byte[1024]; 
    long total = 0; 
    int count = 0; 
    while ((count = is.read(data)) != -1) { 
     total += count; 
     int progress_temp = (int) total * 100/lenghtOfFile; 
     /*publishProgress("" + progress_temp); //only for asynctask 
     if (progress_temp % 10 == 0 && progress != progress_temp) { 
      progress = progress_temp; 
     }*/ 
     fos.write(data, 0, count); 
    } 
    is.close(); 
    fos.close(); 
} catch (Exception e) { 
    Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage()); 
} 
+0

변수'progress'를 초기화 한 후에 알려지지 않은 변수'count'를 참조했습니다. 'count'가'progress'라고 가정하는 것이 안전한가요? – Adam

+0

죄송합니다! 액세스 할 수있는'int count; '를 추가하십시오. 내 대답 편집! – MrYanDao