将数据保存在外部存储器上

 /* 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 文件保存的更多相关文章

  1. android 文件保存到应用和sd卡中

    <span style="font-size:18px;">1.权限添加 <uses-permission android:name="android. ...

  2. Android文件保存和读取

    public class DataActivity extends Activity { private EditText filenameText; private EditText content ...

  3. android如何保存读取读取文件文件保存到SDcard

    android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...

  4. Android数据保存之文件保存

    前言: 上一篇文章写了在Android中利用SharedPreferences保存数据,SharedPreferences在保存数据的时候主要是保存一些应用程序的设置信息或者少量的用户信息,并且是以k ...

  5. Android Developers:保存文件

    Android使用一个和其它平台基于硬盘文件系统相似的文件系统.这个课程描述了如何和在Android文件系统使用File APIs读和写文件. 一个File对象适用于读或者写从头到尾没用中断的大型数据 ...

  6. Android——文件的保存和读取

    Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中.可以使用Context ...

  7. Android 中保存数据到文件中

    1.在安卓开发中,会遇到保存数据到手机中以及从手机中获取数据的情况 /** * 把数据存放到手机内存中 * * @param number * @param password * @return */ ...

  8. android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

      最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...

  9. android文件存储位置切换

    最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占 ...

随机推荐

  1. ie6下按钮下边框消失不显示的问题

    最近网站做改版,又发现一个ie6奇葩的问题,就一个很普通带边框的按钮,但在ie6中下边框不显示,ie7没有测试不知道是不是也不显示,其他浏览器正常 代码和预览效果如下: <style> b ...

  2. percona-toolkit 工具集安装

    下载地址: www.percona.com/downloads/percona-toolkit     安装方法一,源码安装: perl Makefile.PL make:make install   ...

  3. selenium 打开浏览器报错java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/String;)

    java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/Strin ...

  4. Android(java)学习笔记60:继承中父类 没有无参构造

    1. 继承中父类 没有无参构造: package com.himi.test1; /* 如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢? 报错. 如何解决呢? A:在父类中加一个无参构造 ...

  5. 20145238-荆玉茗《Java程序设计》课程总结

    每周读书笔记链接汇总 第一周读书笔记: 第二周读书笔记: 第三周读书笔记: 第四周读书笔记: 第五周读书笔记: 第六周读书笔记: 第七周读书笔记: 第八周读书笔记: 第九周读书笔记: 实验报告链接汇总 ...

  6. 20145238-荆玉茗 《Java程序设计》第6周学习总结

    20145238 <Java程序设计>第6周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·如果要将数据从来源中取出,可以使用输入串流,若将数据写入目的地,可以使用输出串流. ...

  7. Spring注解@Value数值取值转换字符串失败

    配置文件(yml)中,配置项如下: cebconfig: INST_CODE: 08801001 SFT_NOTIFY_CEB_CHANNEL: 123456 期望INST_CODE: 0880100 ...

  8. ajax实现无刷新两级联动DropDownList

    ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...

  9. parameter server

    http://zeromq.org ZeroMQ \zero-em-queue\, \ØMQ\: Ø  Connect your code in any language, on any platfo ...

  10. 当Java遇见了Html--Jsp详解篇

    ###一.什么是Jsp jsp是一种基于文本的程序,全名java server page,其特点是html和java程序共存.执行时jsp会被运行容器编译,编译后的jsp跟servlet一样,因此js ...