- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
目前,我有以下代码用于保存 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/
我使用以下代码来创建和写入文件 FileOutputStream fOut = mcontext.openFileOutput("msg.txt",Context.MODE_WORLD_READAB
我正在尝试读取文本文件。到目前为止,我已经使用 BufferReader 获得了代码。但在我使用 openFileInput("Storyline.txt") 的地方,它会给出错误:The metho
我有这个代码: package com.example.android.game; import android.content.Context; import android.graphics.Ca
我添加了用于文件浏览的外部库。该库返回选定的文件路径键,但不返回值。 package com.example.dev.nordugrid; import com.orleonsoft.android.
我正在尝试为学校项目编写一个基本的“记事本”应用程序。 我用 editText 创建了主类,我将它保存为 String textOutput。 我使用以下方法将字符串保存到文件中: FileOutpu
要从应用程序的私有(private)存储区读取 Android 中的文件,请使用函数openFileInput()。 我的问题是,有没有办法在调用这个函数之前检查这个文件是否存在?该函数可以抛出 Fi
我正在尝试使用 context.getContentResolver().openInputStream(Uri) 打开多个线程的多个文件但似乎在读取时,所有线程都从同一个文件读取。我尝试过使用方法
我正在尝试让我的 rss 阅读器保存 URL 地址,以便在重新打开时保存它。但出于同样的原因,openFileOutput 和 openFileInput 是红色的,它表示它无法解析方法。 我们的教授
所以,我正在尝试从 android internal private storage 加载一个预存储的 XML 文件。并将其解析为 JDOM 文档。 XML 与以下相同;
我正在尝试编写/创建一个文件,但是无论我如何尝试,我都会遇到 filenotfound 异常。我已经尝试了所有这些: FileInputStream is = openFileInput(filena
package com.example.tictactoeshowgrid; import android.os.Bundle; import java
我在代码行中收到“无法解析方法”错误: FileInputStream fis = openFileInput(fileName); 我不参加 Activity 我在一个单独的类(class)所以我假
目前,我有以下代码用于保存 Web 存档,然后将其作为 FileInputStream 获取。但是,webContent 中的 channel 仍然为空,并抛出 FileNotFoundExcepti
我一直在玩弄 android 平台,玩弄不同的数据存储方式。现在我正在使用 Context 方法 openFileInput() 和 openFileOutput()。 正如这两种方法的文档告诉我的那
我有一个 Android Service 每天运行,它会进行一些数据同步。它每天下载一次文件并通过 context.openFileOutput 将其缓存到磁盘: String fileName =
在我的 MainActivity.java 中,我试图根据代码 given here 打开一个文件.但是在运行代码的时候 public class MainActivity extends AppCo
我是一名优秀的程序员,十分优秀!