zoukankan      html  css  js  c++  java
  • android 文件保存

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

     1 /* Checks if external storage is available for read and write */
     2 public boolean isExternalStorageWritable() {
     3     String state = Environment.getExternalStorageState();
     4     if (Environment.MEDIA_MOUNTED.equals(state)) {
     5         return true;
     6     }
     7     return false;
     8 }
     9 
    10 /* Checks if external storage is available to at least read */
    11 public boolean isExternalStorageReadable() {
    12     String state = Environment.getExternalStorageState();
    13     if (Environment.MEDIA_MOUNTED.equals(state) ||
    14         Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    15         return true;
    16     }
    17     return false;
    18 }

    该问外部存储器上的文件前,需要先做如上的判断,此外,需要在应用中添加用户权限:

    <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.

    该类文件仅属于你的应用,应用卸载后,数据删除。例如,应用下载的资源文件等。

    保存文件到内部存储器:

     1 File file = new File(context.getFilesDir(), filename);
     2 
     3 /*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:*/
     4 
     5 String filename = "myfile";
     6 String string = "Hello world!";
     7 FileOutputStream outputStream;
     8 
     9 try {
    10   outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
    11   outputStream.write(string.getBytes());
    12   outputStream.close();
    13 } catch (Exception e) {
    14   e.printStackTrace();
    15 }

    保存文件到外部存储器:

    1 public File getAlbumStorageDir(String albumName) {
    2     // Get the directory for the user's public pictures directory. 
    3     File file = new File(Environment.getExternalStoragePublicDirectory(
    4             Environment.DIRECTORY_PICTURES), albumName);
    5     if (!file.mkdirs()) {
    6         Log.e(LOG_TAG, "Directory not created");
    7     }
    8     return file;
    9 }
    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;
    }
    
    
  • 相关阅读:
    GitBook基本使用
    Window 远程桌面漏洞风险,各个厂家的扫描修复方案(CVE-2019-0708)
    应急响应实战笔记(续)
    不同系统下,复制文件时新文件的日期区别
    Window应急响应(六):NesMiner挖矿病毒
    利用python输出000至999中间的数
    揭秘骗局:这是一张会变的图片
    如何查看github排行热度
    zabbix使用自动发现功能批量监控服务器端口的可用性
    使用python脚本批量设置nginx站点的rewrite规则
  • 原文地址:https://www.cnblogs.com/meizixiong/p/3366575.html
Copyright © 2011-2022 走看看