简体   繁体   English

在Android设备的内部存储中写入/读取文件(文件消失)

[英]Writing / reading file in the internal storage of Android (the file disappears)

I have a class which write a png in the internal storage. 我有一个在内部存储中编写png的类。 When I write and read it just after that, it works. 当我写完之后立即阅读时,它可以工作。

FileOutputStream fileOutStream = openFileOutput(filepath,
                    Context.MODE_PRIVATE);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutStream);
fileOutStream.close();

(the type of Bitmap is a Bitmap) (位图的类型是位图)

FileInputStream fileInStream = openFileInput(filepath);
byte[] fileContent = org.apache.commons.io.IOUtils.toByteArray(fileInStream); 

When I use the same read function, with the same filepath parameter (I verify id), but in another class, it doesn't work. 当我使用相同的读取功能和相同的filepath参数(我验证id)时,但是在另一个类中,它不起作用。

Is there a limitation when using with another class of the same project ? 与同一项目的另一个类一起使用时是否有限制?

Regards 问候

Are you sure there is only one underlying stream on this file? 您确定此文件上只有一个基础流吗? because if you have multiple stream and call close() method, this will enforce to close all other streams.. so correct implementation is to close the last stream or apply flush to each one .. and close the last .. btw: for a single process of stream writing there is no need to explicitly call flush() method, because close() methods will call it implicitly. 因为如果您有多个流并调用close()方法,这将强制关闭所有其他流..因此正确的实现是关闭最后一个流或对每个流应用刷新..并关闭最后一个.btw:流写入的单个过程无需显式调用flush()方法,因为close()方法将隐式调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM