简体   繁体   English

如何使用Android内部存储

[英]How to use Android Internal Storage

I am writing an application where I would like to allow the created csv file to be used on a PC. 我正在编写一个应用程序,希望在PC上使用创建的csv文件。 Android has Internal Storage which is viewable on the Android and can be accessed via USB on the PC. Android具有内部存储,可在Android上查看,并可通过PC上的USB访问。 I tried various methods to generate a file name: 我尝试了多种方法来生成文件名:

/data/user/0/com.ed.ilan.ioioterbium/files/documents/2016_9_16_10_28_12.csv
/data/user/0/com.ed.ilan.ioioterbium/files/2016_9_16_10_35_57.csv
2016_9_16_10_46_34.csv

The name of the application I got using getFilesDir(), but the first 2 examples didn't work as Java complained about the slashes in the name. 我使用getFilesDir()获得的应用程序名称,但是前两个示例不起作用,因为Java抱怨名称中的斜杠。 The 3rd one worked but nothing was visible in (appName)/files/. 第三个工作正常,但在(appName)/ files /中看不到任何内容。 I don't know if this by design for security reasons, or if the file was really not there. 我不知道这是出于安全原因设计的,还是文件确实不存在。 The code is 该代码是

String name = getFileName();
try {
        FileOutputStream fOut = new FileOutputStream(name);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write("test1");
        osw.flush();
        osw.close();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Where I would really like the file to be would be in Internal Storage. 我真正希望将文件保存在内部存储中。 I created a folder called myData, which I can see both on the phone and on the PC. 我创建了一个名为myData的文件夹,可以在手机和PC上看到该文件夹​​。 The question is: how do I direct the application to write to Internal Storage. 问题是:如何指导应用程序写入内部存储。 I recognize that this isn't "secure", but I just want to get the file. 我知道这不是“安全的”,但我只想获取文件。

To write to the Internal storage one needs this in the manifest 要写入内部存储器,清单中需要此

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

If we should decide to put the file on a cloud server, is there a standard way to do that? 如果我们决定将文件放在云服务器上,是否有标准方法可以做到这一点?

Thanks, Ilan 谢谢,伊兰

I understand that you want to write a file to your phone's Documents folder, am I right? 我知道您想将文件写入手机的Documents文件夹,对吗? If so, the following may be of help where you can write your .csv files at. 如果是这样,下面的内容可能会对您有所帮助,您可以在其中写入.csv文件。

 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);

If you are using getFilesDir() then it returns a path which is specific to android application. 如果您使用的是getFilesDir(),则它将返回特定于android应用程序的路径。 User's simply can't access the path returned by getFilesDir(). 用户根本无法访问getFilesDir()返回的路径。

If you have a rooted device then you can get access to the above path. 如果您拥有根设备,则可以访问上述路径。

If you want that only your application can access files stored by you then use getFilesDir() , else you can use 如果希望只有您的应用程序可以访问您存储的文件,则可以使用getFilesDir() ,否则可以使用

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();

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

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