下面的代码通过Environment.getExternalStorageState获取SD卡的状态,如果SD卡不存在或者不可用则提示SD卡不可用,如果可用,则写入数据到SD中的sharejs.com.txt文件
public boolean saveToSD(String tel,String content){ Boolean result = true; if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED){ result = false; Toast.makeText(context,"SD卡不可用",Toast.LENGTH_SHORT).show(); return result; } try { File file = new File(Environment.getExternalStorageDirectory(),"sharejs.com.txt"); FileOutputStream fos = new FileOutputStream(file); String data = tel +":" + content; fos.write(data.getBytes()); fos.flush(); fos.close(); } catch(Exception e) { e.printStackTrace(); Toast.makeText(context,"保存失败"+e.toString(),Toast.LENGTH_SHORT).show(); result = false; } return result; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10690.html