[英]I'm trying to retrieve a file that is stored in internal storage on an Android device but I can't find it
I have written a file to internal storage. 我已将文件写入内部存储。 It shows up in my "data/data/package-name/file folder. It exists when I check in this directory. But in the code below, when I type File file = new File(fileName), file.exists() comes up false. Can anyone tell me why?
它显示在“数据/数据/包名称/文件”文件夹中。当我在该目录中签入时它存在。但是在下面的代码中,当我键入File file = new File(fileName)时,file.exists()出现了假,有人可以告诉我为什么吗?
fileName = "match." + mMatchID + ".json";
Log.d(TAG1, "Filename: " + fileName); // the file name is ok
File file = new File(fileName);
Log.d(TAG1, "Does File Exist: " + file.exists());
Gson gson = new Gson();
if (file.exists()) {
// read file into memory and assign the string to matchString
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
matchString = reader.readLine();
Log.d(TAG1, "When you read the match back into memory: " + matchString);
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG1, "an error occured:" );
} finally{
try {
assert reader != null;
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
match = gson.fromJson(matchString, Match.class);
}
match.setMatchName(mMatchNameEditText.getText().toString());
match.setMatchLevel(mSpinner.getSelectedItem().toString());
// convert match back to JSON
matchString = new Gson().toJson(match);
FileOutputStream fos = null;
try {
fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(matchString.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert fos != null;
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(EditMatchActivity.this, "Match Updated", Toast.LENGTH_SHORT).show();
}
SharedPreferences.Editor editor = getSharedPreferences(MY_GLOBAL_PREFS, MODE_PRIVATE).edit();
editor.putString(CURRENT_MATCH_NAME, mMatchNameEditText.getText().toString());
String level = mSpinner.getSelectedItem().toString();
editor.putString(CURRENT_MATCH_LEVEL, level);
editor.apply();
Here is the evidence in the stack trace: 这是堆栈跟踪中的证据:
08-22 14:17:50.674 2883-2883/com.checkinsystems.ez_score D/EditMatchActivity: Filename: match.08222017PGA5614023.json
08-22 14:17:50.674 2883-2883/com.checkinsystems.ez_score D/EditMatchActivity: Does File Exist: false
08-22 14:17:50.703 2883-3109/com.checkinsystems.ez_score D/EGL_emulation: eglMakeCurrent: 0x7f92842e1d20: ver 2 0 (tinfo 0x7f92842c2f00)
08-22 14:17:50.710 2883-3109/com.checkinsystems.ez_score D/EGL_emulation: eglMakeCurrent: 0x7f92842e1d20: ver 2 0 (tinfo 0x7f92842c2f00)
08-22 14:17:52.684 2883-3109/com.checkinsystems.ez_score E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f92842e25e0
"match." + mMatchID + ".json"
"match." + mMatchID + ".json"
is a simple filename. "match." + mMatchID + ".json"
是一个简单的文件名。 It is not a fully-qualified path to a file. 它不是文件的标准路径。
You need to check a File
using code that matches how you created that file in the first place. 您需要使用与首先创建该文件的方式匹配的代码来检查
File
。 If you used getFilesDir()
or openInputStream()
to create that file, then use new File(getFilesDir(), "match." + mMatchID + ".json")
to create the corresponding File
. 如果使用
getFilesDir()
或openInputStream()
创建该文件,则使用new File(getFilesDir(), "match." + mMatchID + ".json")
创建相应的File
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.