Writing to the internal private storage in Android

柔情痞子 提交于 2019-11-27 22:24:48

Not a direct answer to your question, but nevertheless: I noticed that you don't want to store tons of data in your file. Would it be a sufficient alternative to use the Shared Preferences instead?

And perhaps even more interesting: does the problem occur even when you write to the Shared Preferences file instead?

http://developer.android.com/guide/topics/data/data-storage.html#pref

Panoli

Try changing the following line:

File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");

to:

File newfile = new File(this.getFilesDir() + "/","sample.txt");

I was dealing with the same issue. Finally, I found that you really don't have to give all file path in order to create a new file in internal storage. Just mention the file name and it will be created under your app's package folder at device. I did exactly mentioned here

And it works perfectly. I would say avoid mentioning Full file path like : /data/... in order to create a file (you need right permissions to create a file in such a manner). Let Android framework do the job for creating a private file for your app.

Kingsolmn

A physical device's /data/ directory is only available to the root user. Since the only realistic way to get this access on a physical device is to root the device, DDMS file explorer cannot get into this branch of the directory tree.

As to why the app will not write there, likely the issue is in the fact that I have signed the app with debug keys (not a big *NIX expert, but what appears to be the case here from my personal research).

The internal storage area is sort of private to the application so the user must have root access(rooted device) to create and load the files. If you have rooted device this is how to write a file to the internal storage:

 // Create a file in the Internal Storage

String fileName = "MyFile";
String content = "hello world";

FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

This should instantly create a file called MyFile with the content hello world. The internal storage directory is usually found in a special location specified by our app’s package name. In my case it is /data/data/[package name] and the files created are stored in a directory called files in that directory.

As @bianca says, you're better not using a file path. But instead, use only the filename to create a File. Something like this:

public static void saveTextToFile(Context context, String filename, String content) {
  try {
     FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
     fos.write(content.getBytes());
     fos.close();
  } catch (Exception e) {
     e.printStackTrace();
  }
}

And to get the file, you can use:

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

Read more: Saving Files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!