简体   繁体   English

我正在尝试检索存储在Android设备内部存储中的文件,但找不到

[英]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.

相关问题 无法在文件资源管理器中找到存储在设备内部存储上的文件 - Can't find the file stored on Internal Storage of Device on File Explorer 如何从Android的内部存储中检索位图文件? - How can I retrieve a bitmap file from internal storage in Android? 我正在尝试编写一种方法来从Android设备的内部存储中删除文件,并且 - I am trying to write a method to delete a file from the internal storage in Android device and 如何将文件写入Android上内部存储的文件夹? - How can I write a file to a folder of the internal storage on Android? 在android中的真实设备的内部存储中找不到文件 - Cannot Find the file in internal storage of real device in android 无法将文件写入内部存储 Android - Can't write file to internal storage Android 在哪里可以找到模拟器上的内部存储 - where can I find internal storage on emulator 如何找到存储在手机内部存储器中的 all.mp3 文件? - How can I find all .mp3 files stored on the internal storage of my phone? 我可以写入Android设备上的内部存储空间来检查设备之前是否安装了应用程序吗? - Can I write to internal storage on Android devices to check if app has installed before on the device? 如何从WebView引用存储在内部存储中的文件? - How do I reference a file stored in internal storage from a WebView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM