简体   繁体   中英

How to access to internal storage in Android

I want to store .docx files generated and modified through an Android app into the internal storage of android phones (all the same model of phone if that helps). I've got the loading of the template (located into the app) and the modifying of the fields working, but I'm blocked on how to save the files.

I am using docx4j which has a method for saving :

private void writeDocxToStream(WordprocessingMLPackage template,
        String target) throws IOException, Docx4JException {
            File f = new File(target);
            template.save(f);
}

I have tried other more "manual" ways, but I always have the same problem :

what should be the input for the target directory?

It seems easy to do it for external storage, but concerning internal storage it's something else.

When I simply try to output a String in a test.txt then get the String in it back, with the classical BufferedReader , InputStreamReader , FileInputStream way of doing, I can't get this information whatsoever.

Information on this topic is super scattered, I've passed the afternoon reading about thousands of ways to achieve storage, but none corresponds to my problem.

I have set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in my Manifest just to be sure but it doesn't change anything.

Moreover, and maybe it has to do with the issue, I can't access files (from device or from computer) on my testing device (a Samsung Galaxy 5S which can run the app), I seem to have quite all folders but not any files in them, apart from a few xml and txt files... (However, I still get nothing when doing the : write in EditText - store in File - save File in .txt - retrieve String - view String in textView).

This is super important as I have to retrieve the .docx files for further use, after them being created.

Thank you for your answers and let me know if I can provide any more information, I know my problem is quite unclear.

It seems easy to do it for external storage, but concerning internal storage it's something else.

To write to external storage , you use the two-parameter File constructor, where you provide a File object pointing to some root location on external storage, such as:

new File(getExternalFilesDir(null), target);

To write to internal storage , you use the two-parameter File constructor, where you provide a File object pointing to some root location on internal storage, such as:

new File(getFilesDir(), target);

You don't have to hold the permission android.permission.WRITE_EXTERNAL_STORAGE if you write your file to the app's private internal storage.

In your code, with the call

context.getDir("testfile", Context.MODE_WORLD_READABLE)).getPath().toString()+"/test.docx";

the file will write to the path

/data/data/com.yourpackage.name/app_testfile/test.docx

If the write operate successfully, you can read and retrieve the content of the file by using the path above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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