Writing to the internal private storage in Android

后端 未结 6 2264
粉色の甜心
粉色の甜心 2020-12-06 01:44

I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (Stac

相关标签:
6条回答
  • 2020-12-06 02:19

    I was dealing with the same issue. Finally, I found that you really don't have to give all file paths 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 the 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 write permissions to create a file in such a manner). Let Android framework do the job for creating a private file for your app.

    0 讨论(0)
  • 2020-12-06 02:19

    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.

    0 讨论(0)
  • 2020-12-06 02:29

    Try changing the following line:

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

    to:

    File newfile = new File(this.getFilesDir() + "/","sample.txt");
    
    0 讨论(0)
  • 2020-12-06 02:34

    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

    0 讨论(0)
  • 2020-12-06 02:35

    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).

    0 讨论(0)
  • 2020-12-06 02:35

    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.

    0 讨论(0)
提交回复
热议问题