简体   繁体   English

如何在Android中访问内部存储

[英]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). 我想将通过Android应用程序生成和修改的.docx文件存储到android手机的内部存储中(如果有帮助,请使用所有相同型号的手机)。 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 : 我正在使用docx4j,它具有一种保存方法:

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. 当我只是尝试在test.txt中输出String,然后使用经典的BufferedReaderInputStreamReaderFileInputStream方式将其取回时,无论如何我都无法获得此信息。

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. 我已在清单中设置了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ,只是为了确保它不会改变任何内容。

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). 而且,也许与问题有关,我无法访问测试设备(可以运行该应用程序的Samsung Galaxy 5S)上的文件(从设备还是从计算机),我似乎拥有所有文件夹,但没有它们中的任何文件,除了几个xml和txt文件...(但是,执行以下操作时,我仍然一无所获:在EditText中写入-在文件中存储-在.txt中保存文件-检索字符串-在textView中查看字符串)。

This is super important as I have to retrieve the .docx files for further use, after them being created. 这非常重要,因为在创建.docx文件之后,我必须检索它们以供进一步使用。

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: 要写入外部存储 ,请使用两参数File构造函数,在其中提供指向外部存储上某个根位置的File对象,例如:

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: 要写入内部存储器 ,请使用两参数File构造函数,在其中提供指向内部存储器上某个根位置的File对象,例如:

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. 如果您将文件写入应用的私有内部存储,则无需拥有权限android.permission.WRITE_EXTERNAL_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. 如果写入操作成功,则可以使用上述路径读取和检索文件的内容。

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

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