gpt4 book ai didi

android - context.openFileInput() 在尝试访问存储的文件时返回 null

转载 作者:搜寻专家 更新时间:2023-11-01 08:02:49 25 4
gpt4 key购买 nike

目前,我有以下代码用于保存 Web 存档,然后将其作为 FileInputStream 获取。但是,webContent 中的 channel 仍然为空,并抛出 FileNotFoundException:

        // Save the Web Archive once loading is finished
String path = context.getFilesDir().getAbsolutePath()
+ File.separator + WEB_PREFIX + postId;
webView.saveWebArchive(path);
FileInputStream webContent = null;
try {
webContent = context.openFileInput(WEB_PREFIX + postId);
} catch (FileNotFoundException e) {
Log.d("onPageFinished()", "FileNotFoundException");
e.printStackTrace();
}

如果我尝试改为执行 context.openFileInput(path),我会得到

09-05 23:39:42.448: E/AndroidRuntime(8399): java.lang.IllegalArgumentException: File /data/data/com.example/files/web-2189241737372651547 contains a path separator

有人知道解决办法吗?该文件肯定存在,因为我在上一行中保存了它。

最佳答案

openFileInput() 不接受路径,如果您想访问路径,则只接受文件名。

改用这个:

File file = new File(this.getFilesDir().getAbsolutePath() + (WEB_PREFIX + postId));

编辑:您需要确保从同一个地方保存和检索文件。试一试:

注意:我不确定您是否需要 File.separator,但尝试使用和不使用它,看看哪个有效。

 String path = context.getFilesDir().getAbsolutePath()
+ File.separator + WEB_PREFIX + postId;
webView.saveWebArchive(path);
FileInputStream webContent = null;
try {
webContent = new File(path);
} catch (FileNotFoundException e) {
Log.d("onPageFinished()", "FileNotFoundException");
e.printStackTrace();
}

关于android - context.openFileInput() 在尝试访问存储的文件时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649543/

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