-7

I want to download a file(MP4) by clicking on the list view Items

Please give guidance on this!

Dphi Mrp
  • 21
  • 1
  • 1

1 Answers1

0

onItemClickListener write code to download any file,

int count;
try {
    URL url = new URL("YOUR_URL");
    URLConnection conection = url.openConnection();
    conection.connect();

    int lenghtOfFile = conection.getContentLength();

    InputStream input = new BufferedInputStream(url.openStream(), 8192);
    OutputStream output = new FileOutputStream("DOWNLOAD_LOCATION");

    byte data[] = new byte[1024];
    long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();

} catch (Exception e) {
    e.printStackTrace();
}

Note : You have to write this code in AsyncTask and android.permission.INTERNET is necessary.

Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27