Я пытаюсь загрузить PDF-файл с Google Drive, используя код ниже:
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK){
Log.v(TAG,"server returned http " + urlConnection.getResponseCode()
+ urlConnection.getResponseMessage());
}
else{
Log.v(TAG + "downloading" ,"server returned http " + urlConnection.getResponseCode()
+ urlConnection.getResponseMessage());
}
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
int total = 0;
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
int i = 0;
while((bufferLength = inputStream.read(buffer))!= -1 )
{
i+=1;
Log.v(TAG + "downloading","downloading mega #"+i);
total += bufferLength;
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
inputStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
, как показано в коде, каждая итерация в то время как l oop загружает одну мега, но это занимает 2650 итераций , чтобы скачать только 2 мега файла !!!!!! Есть идеи, как решить эту проблему?