简体   繁体   English

在内部存储中找不到保存到文本文件的数据,如何将文件保存在内部存储 android studio 中?

[英]Cant find the saved data to text file in internal storage, How to save file in internal storage android studio?

I want to create a text file on my android phone and write some text into it.我想在我的 android 手机上创建一个文本文件并在其中写入一些文本。
When I click on the button it says saved to /data/user/0/com.example.savetotextfile/files/example.txt当我单击按钮时,它显示已saved to /data/user/0/com.example.savetotextfile/files/example.txt
But I can't find these folders.但我找不到这些文件夹。
Most apps are in Device storage > Android > data, but the apps that I created myself are not in there.大多数应用程序都在设备存储 > Android > 数据中,但我自己创建的应用程序不在其中。
Am I missing a permission?我错过了许可吗?
I don't want to write to an sd card.我不想写入 SD 卡。

public class MainActivity extends AppCompatActivity {

    private static final String FILE_NAME = "example.txt";

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
    }

    public void save(View v) {
        String text = editText.getText().toString();
        FileOutputStream fos = null;

        try {
            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
            fos.write(text.getBytes());

            editText.getText().clear();
            Toast.makeText(this, "Saved to" + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

If you want to see file saved in internal storage you cannot just see through file manager.如果您想查看保存在内部存储中的文件,您不能只通过文件管理器查看。 (Note your phone should be rooted if you want to see that directly) (请注意,如果您想直接看到,您的手机应该是 root 的)

Follow these steps to view files saved in internal storage.按照以下步骤查看保存在内部存储中的文件。

1) Attach the device to your laptop/computer. 1) 将设备连接到您的笔记本电脑/计算机。

2) Open the android studio. 2)打开android工作室。

3) Now in the right extreme of android studio you will this option Device File Manager. 3) 现在在 android 工作室的右端,您将选择设备文件管理器。

在此处输入图像描述

4) Double click on data folder then again double click on data folder 4)双击数据文件夹,然后再次双击数据文件夹

在此处输入图像描述

5) find the package name of your app like com.example.yourapp and double click on it. 5) 找到您的应用程序的 package 名称,例如 com.example.yourapp 并双击它。

6) double click on files folder under the package name of your app you want to see the internal storage files. 6) 双击您想要查看内部存储文件的应用程序名称 package 下的文件夹。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM