View Full Version : نحوه دانلود عکس
F.zeinali
شنبه 21 اردیبهشت 1392, 11:43 صبح
سلام دوستان
چطور می تونم یه url مشخص کنم بعد بگم برو از از این آدرس عکسو برام دانلود کن ؟
ممون.
نفیسه بی همتا
شنبه 21 اردیبهشت 1392, 16:46 عصر
کدی که در پست بعد قرار دادم عکس را از اینترنت دانلود می کند در کارت حافظه ذخیره می کند
نفیسه بی همتا
شنبه 21 اردیبهشت 1392, 16:49 عصر
سلام
امید وارم مفید واقع بشه
public class AndroidDownloadFileByProgressBarActivity extends Activity {
private ProgressDialog pDialog;
ImageView my_image;
public static final int progress_bar_type = 0;
private static String file_url =" https://encrypted-tbn3.google.com/images?q=tbn:ANd9GcTIQQgqjfdNDRYQ3btnNJwRB07htjMKg eW2FyZrDCkxM7lfikmtHg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
my_image = (ImageView) findViewById(R.id.my_image);
new DownloadFileFromURL().execute(file_url);
}
// __________________________________________________ ____________
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("دانلود تصویر");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORI ZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
// __________________________________________________ _________________
class DownloadFileFromURL extends AsyncTask<String, String, String> {
// _______________________________
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
// ________________________________
@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 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream to write file
OutputStream output = new FileOutputStream(
"/sdcard/downloadedfile.jpg");
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.e("Error: ", e.getMessage());
}
return null;
}
// ______________________________________________
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
// ________________________________________________-
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory()
.toString() + "/downloadedfile.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath( imagePath));
}
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.