0

我像这样存储相机拍摄的照片:

FileOutputStream out = new FileOutputStream("img_example");
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

在 onCreate() 方法(同一个活动/文件)中,我检查文件是否存在,但我一定做错了,因为它没有进入以下测试:

file = getApplicationContext().getFileStreamPath("img_example");
if(file.exists()) 
{
    //doesn't go in here
}

我怀疑这与我给出的路径或上下文有关。


背景信息:我实际上有上述代码的 3 个不同实例。在 file.exists() 测试中,我在“拍摄图像”按钮旁边显示一个勾号。最终,我想在另一个活动中检索图像,但现在我只想检查它是否存在

4

2 回答 2

2

最明显的原因是“因为它不存在”。

我注意到您的代码正在“当前目录”中创建文件,并在应用程序上下文中查找该文件。显然他们不是同一个地方。为什么不只是...

FileOutputStream out = new FileOutputStream(
        getApplicationContext().getFileStreamPath("img_example"));
于 2011-07-19T11:01:08.440 回答
0

正如它在android文档中所说:

getFileStreamPath(String name)

返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的绝对路径。

为什么不使用 openFileOutput() 方法编写文件?

于 2011-07-19T11:00:10.780 回答