【问题标题】:Android: Asus Nexus 7 does not commit emulated memory until restartAndroid:华硕 Nexus 7 在重启之前不会提交模拟内存
【发布时间】:2016-10-26 01:27:17
【问题描述】:

我有一个非常具体的问题 - 我正在尝试写入华硕 Nexus 7 上的外部存储,但它正在写入设备上的模拟目录。

这是我正在使用的代码:

public static void writeExternalMedia(Context context) {
    if(isExternalStorageWritable()) {
        String content = "hello world";

        File filedir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test");
        filedir.mkdir();

        File file;

        FileOutputStream outputStream;

        try {
            file = new File(filedir, "test.txt");

            if (!file.exists()) {
                file.createNewFile();
            }

            outputStream = new FileOutputStream(file);
            outputStream.write(content.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

每当我重新启动设备时,插入时设备下都会出现目录,这是我在执行上述功能时所期望的。

我已尝试寻找解决方案,但找不到问题的答案。

【问题讨论】:

标签: android android-external-storage


【解决方案1】:

我做了两种方法。一个用于创建文件,一个用于附加文件。我认为问题在于您没有调用 createNewFile。

private File CreateFile(String fileName)
{
    File file = new File(this.getFilesDir(), fileName);
    try
    {
        if(!file.exists())
        {
            file.createNewFile();
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return file;
}

private void appendToFile(String file, String content)
{
    try
    {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput(file, this.MODE_APPEND));
        outputStreamWriter.append(content + "\n");
        outputStreamWriter.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

【讨论】:

  • 感谢您的尝试,但没有奏效。我尝试使用的文件目录位于外部存储上。实际写入的目录报告为“/storage/emulated/0/Download/test”。当我将文件写入这个完美运行的模拟目录时,文件直到设备重新启动才会出现。
【解决方案2】:

好吧,经过大量搜索和测试,我终于找到了一个解决方案,通过其他答案之一链接。

https://stackoverflow.com/a/28448843/979220

解决方案是扫描媒体文件,这会导致文件传播到外部存储,而不是留在模拟存储中。

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多