android 文件保存
将数据保存在外部存储器上
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
} /* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
该问外部存储器上的文件前,需要先做如上的判断,此外,需要在应用中添加用户权限:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
<manifest ...>
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
</manifest>
外部存储器与内部存储器的比较:
Internal storage:
- It's always available.
- Files saved here are accessible by only your app by default.
- When the user uninstalls your app, the system removes all your app's files from internal storage.
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.
External storage:
- It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
- It's world-readable, so files saved here may be read outside of your control.
- When the user uninstalls your app, the system removes your app's files from here only if you save them in the directory from
getExternalFilesDir().
External storage is the best place for files that don't require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.
外部存储文件有两类:
public files:
Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user.
For example, photos captured by your app or other downloaded files.
用户和其它app均可访问,当用户卸载该应用后,该类文件不会被移除,例如照片
private files:
Files that rightfully belong to your app and should be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they are files that realistically don't provide value to the user outside your app. When the user uninstalls your app, the system deletes all files in your app's external private directory.
For example, additional resources downloaded by your app or temporary media files.
该类文件仅属于你的应用,应用卸载后,数据删除。例如,应用下载的资源文件等。
保存文件到内部存储器:
File file = new File(context.getFilesDir(), filename); /*Alternatively, you can call openFileOutput() to get a FileOutputStream that writes to a file in your internal directory. For example, here's how to write some text to a file:*/ String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream; try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
保存文件到外部存储器:
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
public File getAlbumStorageDir(Context context, String albumName) {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
android 文件保存的更多相关文章
- android 文件保存到应用和sd卡中
<span style="font-size:18px;">1.权限添加 <uses-permission android:name="android. ...
- Android文件保存和读取
public class DataActivity extends Activity { private EditText filenameText; private EditText content ...
- android如何保存读取读取文件文件保存到SDcard
android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...
- Android数据保存之文件保存
前言: 上一篇文章写了在Android中利用SharedPreferences保存数据,SharedPreferences在保存数据的时候主要是保存一些应用程序的设置信息或者少量的用户信息,并且是以k ...
- Android Developers:保存文件
Android使用一个和其它平台基于硬盘文件系统相似的文件系统.这个课程描述了如何和在Android文件系统使用File APIs读和写文件. 一个File对象适用于读或者写从头到尾没用中断的大型数据 ...
- Android——文件的保存和读取
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中.可以使用Context ...
- Android 中保存数据到文件中
1.在安卓开发中,会遇到保存数据到手机中以及从手机中获取数据的情况 /** * 把数据存放到手机内存中 * * @param number * @param password * @return */ ...
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...
- android文件存储位置切换
最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占 ...
随机推荐
- Linux读取NTFS类型数据盘
Windows的文件系统通常使用NTFS或者FAT32格式,而Linux的文件系统格式通常是EXT系列,请参考下面方法: 1) 在Linux系统上使用以下命令安装ntfsprogs软件使得Linux能 ...
- Do not set "root" as "NOPASSWD" in sudoers file
cat /etc/sudoers root ALL=(ALL)ALL: ALL do not change it to root ALL=(ALL)NOPASSWD: ALL Since ...
- mongdb增加字段和删除字段
增加字段 db.xxx.update({},{"$set":{"column1":1,"column2":0}},false,true); ...
- Codeforces Round #347 (Div.2)_A. Complicated GCD
题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...
- vuejs使用组件的细节点
is属性 <div id='root'> <table> <tbody> <row></row> <row></row&g ...
- 2017.11.2 JavaWeb----第六章 Servlet技术
JavaWeb ------第六章 Servlet技术 (1)在Web应用程序开发中,一般由JSP JavaBean技术和 Servlet技术的结合实现MVC开发模式.在MVC开发模式中将Web程序的 ...
- Linux内存管理 - slab分配器和kmalloc
本文目的在于分析Linux内存管理机制的slab分配器.内核版本为2.6.31.1. SLAB分配器 内核需要经常分配内存,我们在内核中最常用的分配内存的方式就是kmalloc了.前面讲过的伙伴系统只 ...
- scrapy Pipeline 练习
class WeatherPipeline(object): def process_item(self, item, spider): print(item) return item #插入到red ...
- 手动创建maven项目+cmd+webapp+tomcat
1.创建文件夹 2.在刚刚创建的文件夹目录下:创建一个scr文件夹和pom.xml文件 3.在pom.xml配置 <?xml version="1.0" encoding=& ...
- CUDA三维数组
http://hpcbbs.it168.com/forum.php?mod=viewthread&tid=1643 根据上面链接的帖子研究了下三维数组,就像他自己说的一样是有问题的,我自己修改 ...