标签:sdc roi ted equal 应该 sdcard color write ges
1.向sdcard中写入数据:
发现向/mnt/sdcard路径写数据会报错,原因:没有开启相应权限
解决:配置文件中加入
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
或者:
另外:不建议硬编码写成:/mnt/sdcard,应该这样写:
String sdPath = Environment.getExternalStorageDirectory().getPath();
在保存数据到sdcard之前,还应该判断下是否存在sdcard:
if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { String sdPath = Environment.getExternalStorageDirectory() .getPath(); // ...... } else { Toast.makeText(getApplicationContext(), "sd卡不可用", Toast.LENGTH_LONG).show(); }
2.获取sdcard总大小和可用空间:
File file = Environment.getExternalStorageDirectory(); //总大小 long totalSpace = file.getTotalSpace(); //可用大小 long usableSpace = file.getUsableSpace(); //注意返回格式是字节,转换 String formatTotalSpace = Formatter.formatFileSize(MainActivity.this, totalSpace); String formatUsableSpace = Formatter.formatFileSize(MainActivity.this, usableSpace);
标签:sdc roi ted equal 应该 sdcard color write ges
原文地址:https://www.cnblogs.com/xuyiqing/p/8862085.html