简体   繁体   English

在Android中将文件存储在SD卡上

[英]Storing files on SD card in Android

I would like to store a file on the SD card. 我想在SD卡上存储一个文件。 According to the Android docs, SharedPreference can help to retrieve key-value pairs of primitive data types. 根据Android文档, SharedPreference可以帮助检索原始数据类型的键值对。 However, it stores files in the internal storage. 但是,它将文件存储在内部存储中。

Is there a class with similar functionality to SharedPreference but which can store a file on the SD card? 是否有一个类似于SharedPreference但可以在SD卡上存储文件的类?

You first need to check if the SD card is accessable with: 您首先需要检查SD卡是否可以访问:

File sdDir = Environment.getExternalStorageDirectory(); 

Do not forget to put below in your Manifest.xml 不要忘记将下面放在Manifest.xml中

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

You can then use something like this: 然后你可以使用这样的东西:

File file = new File(root, "/sdcard/test.txt");             
FileWriter fwriter = new FileWriter(file);             
BufferedWriter out = new BufferedWriter(fwriter);             
out.write("Hello world");             
out.close(); 

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

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