mrtzxxx
شنبه 17 اسفند 1392, 20:09 عصر
این کد واسه دانلوده ولی رو بعضی از گوشیها مثل galaxy sIII مشکل داره ولی با بعضی دیگه از گوشیها مخصوصا اندروید 2.3 هیچ مشکلی نداره و فایل رو دانلود میکنه اگه از دوستان عزیز کسی میدونه مشکل این کد چیه خواهش میکنم کمک کنه :
class DownloadFileFromURL extends AsyncTask<String, String, String>{
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!cancel){
showDialog(progress_bar_type);
}
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 1024k buffer
InputStream input = new BufferedInputStream(url.openStream(), 1024);
// Output stream to write file
OutputStream output = new FileOutputStream(Environment.getExternalStorageDir ectory().toString()
+ "/baby/"+namefile);
byte data[] = new byte[1024];
long total = 0;
while ((!isCancelled() || (!cancel)) && (count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
if (!isCancelled() || (!cancel)){
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
if (isCancelled() || (cancel)){
pDialog.dismiss();
pDialog = null;
DownloadFileFromURL.this.cancel(true);
pDialog.setProgress(0);
finish();
}
} catch (Exception e) {
// Log.e("ERROR MORTEZA moghe download : ", e.getMessage());
//Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
if (!isCancelled()|| !cancel){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
if (!isCancelled()|| !cancel){
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), " فایل با موفقیت دانلود شد ", Toast.LENGTH_SHORT).show();
start();
}
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>{
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!cancel){
showDialog(progress_bar_type);
}
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 1024k buffer
InputStream input = new BufferedInputStream(url.openStream(), 1024);
// Output stream to write file
OutputStream output = new FileOutputStream(Environment.getExternalStorageDir ectory().toString()
+ "/baby/"+namefile);
byte data[] = new byte[1024];
long total = 0;
while ((!isCancelled() || (!cancel)) && (count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
if (!isCancelled() || (!cancel)){
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
if (isCancelled() || (cancel)){
pDialog.dismiss();
pDialog = null;
DownloadFileFromURL.this.cancel(true);
pDialog.setProgress(0);
finish();
}
} catch (Exception e) {
// Log.e("ERROR MORTEZA moghe download : ", e.getMessage());
//Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
if (!isCancelled()|| !cancel){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
if (!isCancelled()|| !cancel){
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), " فایل با موفقیت دانلود شد ", Toast.LENGTH_SHORT).show();
start();
}
}
}