انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 5 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده !

نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده !

#3
این چیزی که میگید تشخیص بده حجم فایل رو خیلی خوبه
ولی فکر میکنم بهتره به این فکر باشم که اگه دانلود قطع شد کاربر بتونه بعدا فایل رو از ادامه دانلود کنه یعنی همون resum
نمیدونم میشه این کارو کرد یا نه ؟



ببینید من برای چک کردن اتصال اینترنت از این کد استفاده میکنم :



کد:
private boolean isNetworkConnected() {
// check internet connection
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
Toast.makeText(getApplicationContext(), "براي دريافت اين فايل ، يکبار مي بايست به اينترنت متصل شويد", Toast.LENGTH_LONG).show();
return false;
} else
return true;
}

و در دکمه دانلود هم کدهام اینه :



کد:
ImageView imgdownload1 = (ImageView) findViewById(R.id.imageView1);
imgdownload1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
File file = new File(root.getAbsolutePath() + "/filmha/" + "film1.mp4");
if (file.exists() ){
Toast.makeText(getApplicationContext(), "فايل قبلا دانلود شده است", Toast.LENGTH_LONG).show();
return;
}

if(isNetworkConnected()){ // check internet connection // instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(Film.this);
// MainActivity = activity name mProgressDialog.setMessage("فايل در حال دانلود مي باشد"); mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(Film.this);
// MainActivity = activity name
downloadTask.execute("http://www.dl.moralschool.ir/film1.mp4");
// the url to the file you want to download
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) { downloadTask.cancel(true);
File file = new File(root.getAbsolutePath() + "/filmha/" + "film1.mp4");
if (file.exists() ){
file.delete();
Toast.makeText(getApplicationContext(), "فايل دانلود نشد", Toast.LENGTH_LONG).show(); return;
}
}
});
}
}
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here class DownloadTask extends AsyncTask<String, Integer, String> { private Context context;
public DownloadTask(Context context) { this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wl.acquire();
try { InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try { URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
// this will be useful to display download percentage
// might be -1: server did not report the length int fileLength = connection.getContentLength();
// download the file input = connection.getInputStream();
output = new FileOutputStream("/sdcard/filmha/film1.mp4");
// /sdcard/file_name.extension byte data[] = new byte[4096];
long total = 0; int count; while ((count = input.read(data)) != -1) {
// allow canceling with back button if (isCancelled()) return null; total += count;
// publishing the progress.... if (fileLength > 0)
// only if total length is known publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
}
catch (Exception e) { return e.toString();
} finally{
try {
if (output != null) output.close();
if (input != null) input.close();
}
catch (IOException ignored) { } if (connection != null) connection.disconnect();
}
}
finally { wl.release();
} return null;
}
@Override
protected void onPreExecute() { super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) { mProgressDialog.dismiss();
if (result != null) Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"فايل با موفقيت دانلود شد", Toast.LENGTH_SHORT).show();
VideoView vv=(VideoView) findViewById(R.id.videoView1);
vv.setVideoURI(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/filmha/" + "film1.mp4"))); vv.start(); vv.requestFocus();
}
}
});


حالا چیزی که میخوام اینه که اگه اینترنت قطع شد مثلا فایل حذف بشه یعنی کد اینکه چک کنه اینترنت قطع شده چی هست ؟
یا اینکه اگه بشه در صورت قطع شدن اینترنت یا همون چیزی که شما گفتید به هر دلیل و اتفاقی اگه فایل دانلود نشد ، در دانلود مجدد از ادامه فایل رو دانلود کنه که خیلی میتونه بهتر باشه

 
پاسخ


پیام‌های این موضوع
RE: نحوه حذف فایلی که در اثر قطع شدن اینترنت ، ناقص دانلود شده ! - توسط moralschool - ۱۳۹۳/۰۲/۰۵, ۰۷:۴۹ ق.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 2 مهمان