简体   繁体   English

在内部存储上写

[英]Write on Internal Storage

Is there any way to write in internal storage in Android? 有什么办法可以在Android的内部存储中写入内容吗? I tried the following from google developer site- 我在Google开发人员网站上尝试了以下方法-

File file = new File(context.getFilesDir(), filename);

String filename = "myfile";
String fileContents = "Hello world!";
FileOutputStream outputStream;

try {
    outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
    outputStream.write(fileContents.getBytes());
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}

The problem is that whenever I uninstalls the app the file is also deleted. 问题是,每当我卸载该应用程序时,该文件也会被删除。 However I am required to keep that file even after uninstalling the App... 但是,即使卸载应用程序后,我也必须保留该文件...

You have to define which type of file you are try to create like. 您必须定义尝试创建的文件类型。

FileType = "image"; FileType =“ image”; FileType = "text"; FileType =“文本”;

Define filetype in your code. 在代码中定义文件类型。

When you use context.getFilesDir() you're accessing the project files directory. 使用context.getFilesDir()您正在访问项目文件目录。 If you want to save it externally, you should use something like Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) instead and use permissions in your Android Manifest: 如果要从外部保存它,则应改用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)类的东西,并在Android Manifest中使用权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

You can look for other directories here . 您可以在此处查找其他目录。

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

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