我用下面的代碼在我的Android項目在下載文件時:的Android getContentLength總是返回-1下載apk文件
URL url = new URL(fileUrl);
URLConnection conection= url.openConnection();
conection.setDoOutput(true);
conection.connect();
int lenghtOfFile = conection.getContentLength();
如果fileUrl是APK,lenghtOfFile總是返回-1。
但是,如果它是圖像,視頻類型,... lenghtOfFile返回正好。
爲什麼?
我使用Eclipse,Android SDK中的修訂23.
這可能是後端問題 – Blackbelt
您是否驗證過該文件實際存在,並且該服務器實際上是爲其返回一個內容長度? – Sebastian
文件長度應由服務器處理。你的服務器沒有提供。 – hasan83