Closed lwinmoehein closed 4 years ago
It's will affect process time, because of some sites need to get content length manual. So if you want to get the size of a file, you can check like this
private void checkFileSize(XModel xModel){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... voids) {
if (xModel.getUrl()!=null) {
try {
URLConnection connection = new URL(xModel.getUrl()).openConnection();
if (xModel.getCookie() != null) {
connection.setRequestProperty("Cookie", xModel.getCookie());
}
connection.connect();
return calculateFileSize(connection.getContentLength());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null){
//Your file size =>
System.out.println("File Size=> "+s);
}
}
}.execute();
}
private String calculateFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
I worked fine bro,thanks a lot ,it's really helpful
It would be perfect if there is a way to get the size of file and it can be retrieved easily with XModel.