zoukankan      html  css  js  c++  java
  • sdcard

    使用Sdcard注意事项:

    1.权限问题:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    2.硬性编码问题:通过 Environment可以获取sdcard的路径
    Environment.getExternalStorageDirectory().getPath();
    3.使用前需要判断sdcard状态
    if(!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)){
    //sdcard状态是没有挂载的情况
    Toast.makeText(mContext, "sdcard不存在或未挂载", Toast.LENGTH_SHORT).show();
    return ;
    }
    4.需要判断sdcard剩余空间
    //判断sdcard存储空间是否满足文件的存储
    File sdcard_filedir = Environment.getExternalStorageDirectory();//得到sdcard的目录作为一个文件对象
    long usableSpace = sdcard_filedir.getUsableSpace();//获取文件目录对象剩余空间
    long totalSpace = sdcard_filedir.getTotalSpace();
    //将一个long类型的文件大小格式化成用户可以看懂的M,G字符串
    String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
    String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
    if(usableSpace < 1024 * 1024 * 200){//判断剩余空间是否小于200M
    Toast.makeText(mContext, "sdcard剩余空间不足,无法满足下载;剩余空间为:"+usableSpace_str, Toast.LENGTH_SHORT).show();
    return ;
    }


    /data/data: context.getFileDir().getPath();
    是一个应用程序的私有目录,只有当前应用程序有权限访问读写,其他应用无权限访问。一些安全性要求比较高的数据存放在该目录,一般用来存放size比较小的数据。
    /sdcard: Enviroment.getExternalStorageDirectory().getPath();
    是一个外部存储目录,只用应用声明了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>的一个权限,就可以访问读写sdcard目录;所以一般用来存放一些安全性不高的数据,文件size比较大的数据。

  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/wwttsqt/p/6441367.html
Copyright © 2011-2022 走看看