gpt4 book ai didi

java - 为什么我的 file.exists() 总是返回 false?

转载 作者:行者123 更新时间:2023-12-02 08:14:14 25 4
gpt4 key购买 nike

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

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

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

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

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

<小时/>

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

最佳答案

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

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

FileOutputStream out = new FileOutputStream(
getApplicationContext().getFileStreamPath("img_example"));

关于java - 为什么我的 file.exists() 总是返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6746023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com