DOWNLOAD FILE FROM URL ANDROID EXAMPLE:
Download file from url android example is very useful and easy method. And we will use again and again in applications. All the files in the application downloaded from the URL.
Here, i have return simple steps to download files from URL.
1) First, you have to set the URL of the file,
String file_url="";
2)Download URL using async Task,
new DownloadFileFromURL().execute(file_url);
3)Create URLConnection.
URLConnection conection = url.openConnection();
conection.connect();
4)get file length:
int lengthOfFile=connection.getContentLength();
5)Create output file path:
OutputStream output=FileOutputStream("/sdcard/downloadfile.jpg");
6)Write data into the file.
byte data[] = newbyte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
7)set the image file into the imageview
//set path
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
No comments:
Post a Comment