android初学笔记

1、将数据存储到手机上,可以不用绝对路径,而是用上下文---context,获取数据上下文,然后找到相应的存储位置:
    // 保存用户名和密码
    public static boolean saveInfo(Context context, String username,
            String pwd) {
        try {
            String result = username + "," + pwd;
            FileOutputStream fileOutputStream = context.openFileOutput(
                    "infoo.txt", 0);
            fileOutputStream.write(result.getBytes());
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
 
// 读取用户名和密码
    public static Map<String, String> readInfo(Context context) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            FileInputStream fileInputStream = context.openFileInput("infoo.txt");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(fileInputStream));
            String result = bufferedReader.readLine();
            String[] str = result.split(",");
            map.put("username", str[0]);
            map.put("pwd", str[1]);
            fileInputStream.close();
            return map;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
 
2、将数据保存到外部存储器上sdcard---不能直接报存,首先要先为项目添加权限 android.permission.WRITE_EXTERNAL_STORAGE ,然后才能保存,此时保存数据,也不必用绝对路径,即可以用相对路径:      
// 保存用户名和密码
    public static boolean isRemember(String username, String pwd) {
        try {
            String result = username + "," + pwd;
            // 创建一个文件对象
            File file = new File(Environment.getExternalStorageDirectory()
                    .getPath(), "infoo.txt");
            // 如果是想保存之前的数据,继续往文件里追加数据的话,在new FileOutputStream(file,true),即可
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(result.getBytes());
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }                            
 
// 读取用户名和密码
    public static Map<String, String> readInfo() {
        Map<String, String> map = new HashMap<String, String>();
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(
                    Environment.getExternalStorageDirectory().getPath(),
                    "infoo.txt"));
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(fileInputStream));
            String result = bufferedReader.readLine();
            String[] str = result.split(",");
            map.put("username", str[0]);
            map.put("pwd", str[1]);
            fileInputStream.close();
            return map;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }         
3、同样,在写拨号器的时候,也涉及到授予权限的问题,要授予项目一个CALLPHONE权限。
 
4、获取sdcard的总容量和可用容量,出现让人迷惑的结果:
    解释:你的apk装到哪个盘里,那个盘就认为是外部存储器,这么说,不是很准确。
可以这么说,除了你的后来装到手机上的内存卡叫sdcard外,手机本身也有sdcard的成分,手机总的存储空间分为系统空间和手机U盘空间两部分,后者手机U盘空间就是sdcard。
从你的试验中知道,如果不插内存卡(我们都知道的sdcard),还是可以获取到sdcard的总存储容量和可用容量的,这个时候,总存储容量应该是你手机总的存储容量减去系统容量;
如果,插上内存卡,但是,你把apk安装到了手机上,而不是内存卡上,那么实验结果应该是和你不插内存卡的情况一样,但若是,你把apk安装到内存卡上,这个时候,你获取的总容量就是你内存卡的总量,可用容量就是你内存卡的可用容量。
//代码
        //获取sdcard的总空间和可用大小
        File file = Environment.getExternalStorageDirectory();
        long total = file.getTotalSpace();
        long useable = file.getUsableSpace();
 
        //转换数据格式
        String totalNew = Formatter.formatFileSize(this, total);
        String useableNew = Formatter.formatFileSize(this, useable);
    
 
5、Android下文件权限问题
        
        
 
    当然也可以手动修改文件的权限:(chmod    777  private.txt----变成可读可写可执行)
 
6、为按钮创建点击事件的时候,一定记得方法的参数(View v)!!!
        例如:
       /**
     * 点击这个按钮生成xml文件
     */
    public void click(View v) {
        // 组拼xml文件
        StringBuffer sb = new StringBuffer();
        // 头部信息
        sb.append("<?xml version="1.0" encoding="utf-8"?>");
        // 根节点
        sb.append("<message>");
        // 循环录入各个小节点
        for (Message ms : list) {
            sb.append("<messa>");
            // name节点
            sb.append("<name>");
            sb.append(ms.getName());
            sb.append("</name>");
            // city节点
            sb.append("<city>");
            sb.append(ms.getCity());
            sb.append("</city>");
            // address节点
            sb.append("<address>");
            sb.append(ms.getCity());
            sb.append("</address>");
 
            sb.append("</messa>");
        }
        sb.append("</message>");
 
        // 将xml文件存储到sdcard中
        try {
            File file = new File(Environment.getExternalStorageDirectory()
                    .getPath(),"MESSAGE.xml");
            OutputStream outputStream = new FileOutputStream(file);
            outputStream.write(sb.toString().getBytes());
            outputStream.close();
            // 提示一下用户
            // Toast.makeText(MainActivity.this, "xml文件已经成功保存", 1).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/ZT-SummerRain/p/6735867.html