我的代码有问题。我正在尝试从 url 保存 PDF。PDF 在那里,但它的长度为零。虽然输入流长度是正确的。为什么 PDF 不能正确保存?
这是从流中保存 PDF 的实际代码:
public void createExternalStoragePrivateFile
(String name, String extension, InputStream is) {
// Create a path where we will place our private file on external
// storage.
File file = new File
(mContext.getExternalFilesDir(null), name +"."+extension);
try {
Log.d("", "before: " + file.getPath() +
" " + file.getTotalSpace() + " " + file.length());
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
Log.d("","after: " + file.getPath() +
" " + file.getTotalSpace() + " " + file.length());
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
输出给了我这个(前后长度为零,但之后总空间不为零。为什么长度为零?
04-25 09:38:44.938: D/(4103): before: /storage/emulated/0/Android
/data/what.to.use.myapp/files/Lolpdf.pdf 0 0
04-25 09:38:44.946: D/(4103): after: /storage/emulated/0/Android
/data/what.to.use.myapp/files/Lolpdf.pdf 14311309312 0