当我运行这些方法时,我的程序将被粉碎
我想将pdf文件长度作为String并在TextView上设置此文本
private String getSize(String url1){
URL url = null;
try {
url = new URL(url1);
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection connection = null;
try {
connection = url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
final int length = connection.getContentLength();
String size = "" + length;
return size;
}
答案 0 :(得分:1)
使用以下代码获取文件大小:
URL url = new URL("http://testserver.com/file.pdf");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int fileSize = urlConnection.getContentLength();
textView.setText(String.valueOf(fileSize));
答案 1 :(得分:0)
尝试使用URLConnection.getContentLength()
,如下所示。
URL url = new URL("http://your_url.com/filename.pdf");
final URLConnection connection = url.openConnection();
final int length = connection.getContentLength();
Log.d(tag, String.valueof(length));
你的答案: -
private String getSize(String url1){
URL url = null;
try {
url = new URL(url1);
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection connection = null;
try {
connection = url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
final int length = connection.getContentLength();
String size = "" + String.valueof(length);
return size;
}
请记住在清单中给予INTERNET权限: -
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>