-
-
Save p4yam/8c0e9cff6fd0bfed759239a0a3c5f387 to your computer and use it in GitHub Desktop.
download file2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.kivee.androiddownloadingfile; | |
import android.app.Dialog; | |
import android.app.ProgressDialog; | |
import android.graphics.drawable.Drawable; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import java.io.BufferedInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class MainActivity extends AppCompatActivity { | |
Button button; | |
private ProgressDialog progressDialog; | |
ImageView imageView; | |
public static final int progressType = 0; | |
// آدرس عکس | |
private static String url = "http://cdn.persiangig.com/preview/70hPcBEnKG/DownloadImageorFile.jpg"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
button = (Button) findViewById(R.id.download_button); | |
imageView = (ImageView) findViewById(R.id.imageView); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// اسنک تسک صدا زده میشه | |
new DownloadFromURL().execute(url); | |
} | |
}); | |
} | |
class DownloadFromURL extends AsyncTask<String, String, String> { | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
showDialog(progressType); | |
} | |
@Override | |
protected String doInBackground(String... fileUrl) { | |
int count; | |
try { | |
URL url = new URL(fileUrl[0]); | |
URLConnection urlConnection = url.openConnection(); | |
urlConnection.connect(); | |
// نمایش پروگرس بار | |
int fileLength = urlConnection.getContentLength(); | |
InputStream inputStream = new BufferedInputStream(url.openStream(), 8192); | |
OutputStream outputStream = new FileOutputStream("/sdcard/downloadedfile.jpg"); | |
byte data[] = new byte[1024]; | |
long total = 0; | |
while ((count = inputStream.read(data)) != -1) { | |
total += count; | |
publishProgress("" + (int) ((total * 100) / fileLength)); | |
outputStream.write(data, 0, count); | |
} | |
outputStream.flush(); | |
outputStream.close(); | |
inputStream.close(); | |
} catch (Exception e) { | |
Log.e("Error: ", e.getMessage()); | |
} | |
return null; | |
} | |
// آپدیت کردن پروگرس دیالوگ | |
protected void onProgressUpdate(String... progress) { | |
// نمایش درصد دانلود | |
progressDialog.setProgress(Integer.parseInt(progress[0])); | |
} | |
@Override | |
protected void onPostExecute(String file_url) { | |
dismissDialog(progressType); | |
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; | |
imageView.setImageDrawable(Drawable.createFromPath(imagePath)); | |
} | |
} | |
//progress dialog | |
@Override | |
protected Dialog onCreateDialog(int id) { | |
switch (id) { | |
case progressType: // در هنگام شروع باید ۰ باشد | |
progressDialog = new ProgressDialog(this); | |
progressDialog.setMessage("File is Downloading. Please wait..."); | |
progressDialog.setIndeterminate(false); | |
progressDialog.setMax(100); | |
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); | |
progressDialog.setCancelable(true); | |
progressDialog.show(); | |
return progressDialog; | |
default: | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment