指定したurlアドレスからファイルをダウンロードし、実測して使用できます.
中にはandroid studioが自動的に追加されているものもあり、実測が可能で、自分が使っている間にpublic class FileUrlDownloadUtil{
/**
* : URL
* @param urlPath
*
* @param downloadDir
*
* @return
*/
@SuppressWarnings("finally")
public static File downloadFile(String urlPath, String downloadDir) {
File file = null;
try {
//
URL url = new URL(urlPath);
// ,
URLConnection urlConnection = url.openConnection();
// http
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
//
httpURLConnection.setConnectTimeout(1000*5);
// , GET
httpURLConnection.setRequestMethod("POST");
//
httpURLConnection.setRequestProperty("Charset", "UTF-8");
// URL ( )。
httpURLConnection.connect();
//
int fileLength = httpURLConnection.getContentLength();
//
System.out.println(" :" + fileLength / (1024 * 1024) + "MB");
//
URLConnection con = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
// ( )
String fileFullName="";
if(urlPath.contains("=")){
fileFullName=urlPath.substring(urlPath.lastIndexOf("=")+1);
}
// ( )
String path = downloadDir + File.separatorChar + fileFullName;
file = new File(path);
// ,
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(file);
int size = 0;
int len = 0;
byte[] buf = new byte[2048];
while ((size = bin.read(buf)) != -1) {
len += size;
out.write(buf, 0, size);
//
System.out.println(" -------> " + len * 100 / fileLength + "%
");
}
//
bin.close();
out.close();
System.out.println(" !");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(" !");
} finally {
return file;
}
}