Download file from url android example

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 to 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));
Download

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top