码迷,mamicode.com
首页 > 移动开发 > 详细

[android] 保存文件到手机内存

时间:2016-02-28 22:53:32      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:

1. 界面的准备工作,普通登录界面,采用线性布局和相对布局。

<Checkbox/>有个属性 android:checked=”true”,默认选中状态,相对布局里面<Button/>位于右边android:layout_alignParentRight=”true”,位于父控件的右面。密码框星号显示android:inputType=”textPassword”

 

2. 遇到device not found等错误可以直接忽略掉,布局文件属性里面绑定点击方法,传入的参数View对象代表当前按钮,控件首先都声明在Activity的成员属性里面,在onCreate()方法里面初始化,初始化控件一定要在setContentView()方法加载完界面之后才行。

 

3. 复选框判断是否选中使用CheckBox对象的isChecked()方法,判断字符串是否相等用String对象的equals()方法,logcat如果无法打印日志,关闭logcat重开或者关闭eclipse

 

4. 保存文件javaSE里面是直接new File(“aaa.txt”),文件默认保存在工程的目录下面,但是在android系统里面,这样默认是创建在/data/app 目录下面,这里是不允许创建文件的。Android下每一个应用都有自己的数据文件夹/data/data/包名/

 

5. 新建一个业务类来处理保存信息的操作。这里的写法和javaSE一样,new File(“/data/data/包名/文件名”)对象,new FileOutputStream() 对象,此时会有异常抛出,因为我们这个方法有返回boolean值,所以我们捕获掉,如果是无返回值那就throws Exception抛出去。字符串信息getBytes()转成字节数组,调用foswrite()方法,关闭fos。当这个方法没有使用类的成员属性的时候,谷歌推荐把这个方法定义成static静态的,效率更高

 

6. 文件路径部分,如果按照上面所写,灵活性很差。当我改变包名的时候,程序会报错,R文件要从新导一下,并且android会认为是个新的应用。谷歌提供了一个api来获取应用的数据目录,调用Context上下文对象的getFilesDir()方法,返回的是/data/data/包名/files/。因此可以这样new File(context.getFilesDir(),"info1.txt");来写。

 

7. 数据目录还有个文件夹是cache目录,调用Context对象的getCacheDir()来获取,这个目录可以通过设置里面清除缓存清掉,这个目录不能存放过大的文件

 

8. 上下文就是一个类提供了方便的api可以得到应有程序的环境,可以获取包名,文件路径,资源路径,资产路径等

 

9. 读取保存文件的信息,同样new File() ,new FileInputStream() ,

 

activity代码:

 

package com.tsh.savefile;

import java.util.Map;

import org.w3c.dom.Text;

import com.tsh.savefile.service.LoginService;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox cb_rember;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username=(EditText) findViewById(R.id.et_username);
        et_password=(EditText) findViewById(R.id.et_password);
        cb_rember=(CheckBox) findViewById(R.id.cb_rember);
        //读取
        Map<String, String> info=LoginService.getSavedUserInfo(this);
        if(info != null){
            et_username.setText(info.get("username"));
            et_password.setText(info.get("password"));
        }
    }
    /**
     * 登陆
     * @param v
     */
    public void login(View v){
        String username=et_username.getText().toString().trim();
        String password=et_password.getText().toString().trim();
        if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
        }
        //记住密码
        if(cb_rember.isChecked()){
            Boolean res=LoginService.saveUserInfo(this,username, password);
            if(res){
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
            }
        }
        //验证
        if(username.equals("taoshihan")&&password.equals("1")){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}

 

业务类代码:

package com.tsh.savefile.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

public class LoginService {
    /**
     * 保存用户名和方法的业务方法
     * @param context 上下文
     * @param username 用户名
     * @param password 方法
     * @return
     */
    public static boolean saveUserInfo(Context context,String username,String password){
        File file=new File(context.getFilesDir(),"info1.txt");
        try {
            FileOutputStream fos=new FileOutputStream(file);
            String info=username+"##"+password;
            fos.write(info.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
    /**
     * 读取
     * @return
     */
    public static Map<String,String> getSavedUserInfo(Context context){
        File file=new File(context.getFilesDir(),"info1.txt");
        try {
            FileInputStream fis=new FileInputStream(file);
            BufferedReader br=new BufferedReader(new InputStreamReader(fis));
            String[] res=br.readLine().split("##");
            Map<String, String> map=new HashMap<String,String>();
            map.put("username", res[0]);
            map.put("password", res[1]);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        
    }
}

layout代码:

<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"
    android:orientation="vertical"
    tools:context="com.tsh.savefile.MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录名" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密码" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb_rember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:checked="true"
            android:text="记住密码" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="登陆" />
    </RelativeLayout>

</LinearLayout>

 

[android] 保存文件到手机内存

标签:

原文地址:http://www.cnblogs.com/taoshihan/p/5225678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!