Android下获取SD卡的状态并写入文件到SD卡

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

下面的代码通过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;
    }