私は何かをテストしています。
packages/apps/Camera/ にassetsフォルダーを作成し、フォルダーにtest.txtファイルを追加しました。
しかし、次のコード フラグメントに従ってonCreate()メソッドでファイルにアクセスすると、ファイルを取得できないことがわかりました。
File file = new File("/assets/test.txt");
BufferedReader reader = null;
try {
Log.v("jerikc","read the file");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
Log.v("jerikc","line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
Log.v("jerikc","exception");
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
ログは次のとおりです。
V/jerikc (3454): ファイルを読む
V/jerikc (3454): 例外
間違ったパス ("/assets/test.txt") を追加したと思います。では、正しい道は何ですか?
その他の情報:
私の実際のコードが Util クラスである場合、コンテキストはありません。コンテキストを追加すると、コード構造が大きく変わります。
ありがとう。