Android读取本地存储和SD卡文件
2016-08-22 16:16
435 查看
功能:判断内置sd卡(本地内存)和sd卡是否有某文件。我这里是如果有某文件,按钮显示,如果本地和sd卡都没有,则隐藏。
判断是否存在,如果本地存在,直接返回true,按钮显示;如果本地没有,再去sd卡找。
btn_main.setVisibility(isShow() ? View.VISIBLE : View.GONE);在是否需要显示的地方判断。
public boolean isShow() { //判断sd卡是否存在,并且是否具有读写权限 File file = new File(Environment.getExternalStorageDirectory(), "ZYSJ/Offline/zy.tpk"); if (file.exists()) { AppConstant.OfflineMapPATH = file.getAbsolutePath(); return true; } file = getSDCard(); if (file != null) { AppConstant.OfflineMapPATH = file.getAbsolutePath(); return true; } } return false; }
判断是否存在,如果本地存在,直接返回true,按钮显示;如果本地没有,再去sd卡找。
/** * 获取外置SD卡路径 * * @return 应该就一条记录或空 */ public List<String> getExtSDCardPath() { List<String> lResult = new ArrayList<String>(); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("mount"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { if (line.contains("extSdCard")) { String[] arr = line.split(" "); String path = arr[1]; File file = new File(path); if (file.isDirectory()) { lResult.add(path); } } } isr.close(); } catch (Exception e) { } return lResult; } private File getSDCard() { List<String> cardPath = getExtSDCardPath();//这个方法是获取sd卡的路径个数 File file = null; if (cardPath != null && cardPath.size() >= 1) {//可能不止一张卡,循环遍历查找 File path = new File(cardPath.get(0), "ZYSJ/Offline/zy.tpk"); if (path.exists()) { file = path; } } return file; }
//文件路径 public static String OfflinePATH = null;在AppConstant里面定义。进行上述那些判断的原因是如果检测到本地或sd卡已经存在,则把之前保留的直接赋值就好,无需每次执行查询代码。
相关文章推荐
- Android文件的保存(本地和SD卡)与读取
- Android手机数据读写方法(内部存储、SD卡,网络加载,包内文件读取)
- Android本地数据的存储和读取(文件格式)
- Android文件的保存(本地和SD卡)与读取
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- Android读取手机根目录文件即本地SD卡
- Android本地文件存储,机身和外置sd卡
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- Android 数据存储与读取:文件
- android读取sd卡文件的工具类
- android操作文件,保存在手机自带存储上和保存在SD卡上
- android(安卓)sd卡写入 读取文件的例子
- Android 学习 应用使用大文件的方法 读取sd卡的数据库
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 客户端读取本地文件的数据,发送到服务器,服务器接收并存储到文件中
- Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数
- Unity3D IOS Android支持中文与本地文件的读取写入
- Android往SD卡上存储文件
- Unity3D研究院之IOS Android支持中文与本地文件的读取写入(二十七)
- Android 读取本地(SD卡)图片