zoukankan      html  css  js  c++  java
  • Android开发学习——android数据存储

     

    Android的存储

    •  Android中的数据存储方式及其存储位置

                      

                         

    • SharedPrefrence存储
         1). 位置
              /data/data/packageName/shared_prefs/xxx.xml
         2). 特点
              a. 小数据 key--value
              b. 应用卸载时会自动删除此数据
         3). 相关API

              

          接口/类:
              1). 它是用来做什么的
              2). 如何得到它的对象? 不是new, 而是通过别的对象得到
              3). 能用对象做什么事?  有哪些常用的方法
    •  手机内部文件存储
         1). 位置
              /data/data/packageName/files/文件
         2). 特点
              a. 较大的数据/图片
              b. 应用卸载时会自动删除此数据
         3). 相关API
              
    • 手机外部文件存储
         1). 位置
                
         2). 特点
     
         3). 相关API

                

       比较内部文件与外部文件存储?
         1). 存储空间的大小
         2). 是否是私有的
         3). 应用卸载是否自动删除
    •  Sqlite数据库存储
    1). 路径
         /data/data/packageName/databases/xxx.db
    2). 特点
         存储有一定关系结构的数据
         应用卸载是自动删除
    3). sqlite的特点
         小
         快
     
    4). 相关API

          

            

               

    5). Android单元测试

         

    • 远程服务器存储
                1). 存储的位置
                      远程服务器上
               
              2). 特点
                    1). 必须联网请求
                    2). 只能在分线程执行
                    3). 需要声明权限
             
    3). 实现联网请求的技术
         1). 原生的: HttpUrlConnection/URL
         2). 包装的: HttpClient/HttpGet/HttpPost/HttpResponse/HttpEntity
         3). 框架: Volley/Xutils (异步网络请求)
              a. 不需要我们启动分线程, 框架内部接收到请求后自动会在分线程
              b. 如何返回给你结果数据?   切换到主线程调用监听器的回调方法
    4). HTTP协议:
    5). 搭建服务器端
    a. 安装并配置tomcat
    b. 将tomcat关联eclipse中
    c. 创建一个动态的web project
    d. 把需要直接访问的资源(jsp/apk/image)放在WebContent下
    c. 可能需要创建Servlet/Filter
    d. 运行
    f. 访问:   http://ip:8080/xxxx/index.jsp
    6).实现联网请求功能的3步:
         a. 主线程, 显示提示视图(ProgressDialog/ProgressBar)
         b. 分线程, 联网请求, 并得到响应数据
         c. 主线程, 显示数据


    文件访问权限
    * 指的是谁能访问这个文件
    * 在Android中,每一个应用,都是一个独立的用户
    * 使用10个字母表示  drwxrwxrwx
    * 第一个字母:
        * d:表示文件夹
        * -:表示文件
    * 第一组rwx:表示的是文件拥有者(owner)对文件的权限
        * r:read,读
        * w:write
        * x:execute
    * 第二组rwx:表示的是跟文件拥有者属于同一用户组的用户(grouper)对文件的权限
    * 第三组rwx:表示的其他用户(other)对文件的权限

    内部存储空间中读写文件

    小案例:用户输入账号密码,勾选“记住账号密码”,点击登录按钮,登录的同时持久化保存账号和密码

    布局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/et1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="用户名:" />
        
        <EditText
            android:id="@+id/et2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="密   码:" />
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住用户名和密码" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="login"
            android:text="登     录" />
    
    </LinearLayout>
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    
       public void login(View v){
           EditText et1 = (EditText) findViewById(R.id.et1);
           EditText et2 = (EditText) findViewById(R.id.et2);
           
           String username = et1.getText().toString();
           String pwd = et2.getText().toString();
           // 判断用户是否勾选保存账号密码
           CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
           if(cb.isChecked()){
               //将用户名和密码写到本地文件,用IO流来写
               File file = new File("data/data/com.example.cunchu/info.txt");//内部存储空间的路径
               FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                fos.write((username+"####"+pwd).getBytes());
                fos.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              
           }
           System.out.print("登录成功!!!");
           Toast.makeText(this,"登录成功!!!", Toast.LENGTH_SHORT).show();
       }
        
    }
    
    public void read(){
            try {
                FileInputStream  fis = new FileInputStream("data/data/com.example.cunchu/info.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String text = br.readLine();
                String []s = text.split("####");
                EditText et1 = (EditText) findViewById(R.id.et1);
                EditText et2 = (EditText) findViewById(R.id.et2);
    //读取到数据之后,回显至输入框 et1.setText(s[0]); et2.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }

    运行结果:

    使用路径api读写文件
    getFilesDir()得到的file对象的路径是data/data/com.itheima.rwinrom2/files
        * 存放在这个路径下的文件,只要你不删,它就一直在
    getCacheDir()得到的file对象的路径是data/data/com.itheima.rwinrom2/cache
        * 存放在这个路径下的文件,当内存不足时,有可能被删除

    系统管理应用界面的清除缓存,会清除cache文件夹下的东西,清除数据,会清除整个包名目录下的东西

    如果有时需要直接复制项目
    需要改动的地方:
        * 项目名字
        * 应用包名
        * R文件重新导包

    外部存储空间中读写文件

       在内部存储读写和外部存储 读写 文件,都是用IO流读写,不同的是,路径不同。

  • 相关阅读:
    (转)MapReduce源码分析总结
    Linux SSH远程文件/目录传输命令scp
    Hadoop学习总结:MapReduce的过程解析
    Python 3 的新特性zz
    Tutorial Learn Python in 10 minutes[zz]
    Hadoop学习总结:Hadoop的运行痕迹
    Python 绝对简明手册
    Linux命令总结
    [Error] 'strlen' was not declared in this scope
    养成C#编程好习惯
  • 原文地址:https://www.cnblogs.com/mengxiao/p/6015523.html
Copyright © 2011-2022 走看看