ورود

View Full Version : حجم فایل در حال دانلود چقدره ؟



Amin-rz
یک شنبه 04 آبان 1393, 13:51 عصر
سلام.
از این کد برای دانلود یه فایل zip استفاده می کنم که پرورگرس بار آپدیت نمیشه.
رو صفر میمونه و وقتی دانلود تموم شد میره رو 100 درصد.
می خوام حجم فایل در حال دانلود رو بدونم اما lenghtOfFile یا صفر برمیگردونه یا 1



// Async Task Class
class DownloadZipfromInternet extends AsyncTask<String, String, String> {

// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method

prgDialog.setMessage("در حال دریافت");
prgDialog.show();

}

// Download Music File from Internet
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);
// Output stream to write file in SD card
OutputStream output = new FileOutputStream(Environment.getExternalStorageDir ectory().getPath()+"/new.zip");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress("" + (int) ((total * 100) / lenghtOfFile));

// Write data to file
output.write(data, 0, count);
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}

// While Downloading Music File
protected void onProgressUpdate(String... progress) {
// Set progress percentage

prgDialog.setMessage("در حال دریافت");
prgDialog.setProgress(Integer.parseInt(progress[0]));

}

// Once Music File is downloaded
@Override
protected void onPostExecute(String file_url) {

prgDialog.dismiss();
}
}

storm_saeed
یک شنبه 04 آبان 1393, 18:06 عصر
منطقتون درسته
این یه کدی بود که نوشته بودم تا فایل زیپ رو بگیره
try {


URL url = new URL(params);


HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();


urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(mConnectionTimeOut );
urlConnection.setReadTimeout(mReadTimeout);
urlConnection.connect();


File file = new File(mDefaultCacheDir, mZipName);
InputStream inputStream = urlConnection.getInputStream();


FileOutputStream fileOutput = new FileOutputStream(file);


int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;


byte[] buffer = new byte[16 * 1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
downloadedSizePercentage = (int) (downloadedSize * 100)
/ totalSize;
publishProgress(downloadedSizePercentage);


}
fileOutput.flush();
fileOutput.close();
inputStream.close();


}

file هم فایل دلبخواهیه
mReadTimeout = 5000
فقط دوتا نکته اولا بافرتون رو 1024 نگیرید کم کم 8K یا 16K بگیرید دوما به publishProgress بهتره int بفرستید یعنی دارید کار اضافه میکنیم به string تغییرش میدید بعد تو onProgressUpdate دارید intاش رو میگیرید