2017-12-11 20 views

答えて

0

任意のファイルをダウンロードするonItemClickListener書き込みコード、

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(); 
} 

注:AsyncTaskandroid.permission.INTERNETにこのコードを記述する必要が必要です。

関連する問題