Чтобы добавить какое-либо кэширование в приложение Android, я пытаюсь записать InputStream
, полученное из myUrl.openConnection().getInputStream()
, в файл.
Это метод, который я написал:
public static void saveInputStream(InputStream inputStream) throws IOException, Exception {
FileOutputStream out = null;
OutputStream os = null;
try {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "feedData.txt";
File myFile = new File(baseDir + File.separator + fileName);
out = new FileOutputStream(myFile, false);
os = new BufferedOutputStream(out);
byte[] buffer = new byte[65536];
int byteRead = 0;
while ((byteRead = inputStream.read(buffer)) != -1) {
Log.d("CacheManager", "Reading.......");
os.write(buffer, 0, byteRead);
}
} catch(IOException e) {
e.printStackTrace();
throw new IOException();
} catch(Exception e) {
throw new Exception();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new IOException();
}
}
}
}
Вызывающая часть выглядит следующим образом:
URL feedUrl = new URL("rss_feed_url");
InputStream inputStream = feedUrl.openConnection().getInputSream();
CacheManager.saveInputSteam(inputStream);
Я получаю следующие исключения:
(23704): Pas de Notification
W/System.err(24015): java.io.IOException: Stream is closed
Что это такое, что закрыто?
Есть идеи?
Спасибо!