Hamed_razor
دوشنبه 27 اردیبهشت 1395, 15:08 عصر
دوستان سلام
من با این کد میتونم فایلی رو که میخوام دانلود کنم ولی نمیدونم چطور قابلیت Stop و Pause بهش اضافه کنم
مثلا یه بخش از فایل رو بتونه بعد دانلود کنه
int count;
try {
URL url = new URL("http://0up.ir/do.php?downf=Zire-Baran-Bayad-Raft.mp3");
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream(Environment.getExternalStorageDir ectory() + "/audio.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.i("MSG: ", e.toString());
}
return null;
من با این کد میتونم فایلی رو که میخوام دانلود کنم ولی نمیدونم چطور قابلیت Stop و Pause بهش اضافه کنم
مثلا یه بخش از فایل رو بتونه بعد دانلود کنه
int count;
try {
URL url = new URL("http://0up.ir/do.php?downf=Zire-Baran-Bayad-Raft.mp3");
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream(Environment.getExternalStorageDir ectory() + "/audio.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.i("MSG: ", e.toString());
}
return null;