Android——数据存储:手机内部存储

存取字符串和存取图片不相同

xml

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_3"
        android:hint="要存储的的内容"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_4"
        android:hint="从文件中读取的内容"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="保存"
            android:layout_weight="1"
            android:onClick="onclick3"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="获取"
            android:layout_weight="1"
            android:onClick="onclick4"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="保存文件"
            android:layout_weight="1"
            android:onClick="onclick5"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="获取文件"
            android:layout_weight="1"
            android:onClick="onclick6"/>
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/maps"
        android:id="@+id/iv_3"/>

java

 //定义文件名
    //定义常量,一般全大写
    final String FILENAME = "test.txt";

    //手机文件存储
    public void onclick3(View view)
    {
        //1.获取要存储的内容
        String content = et_3.getText().toString();
        //2.获取输出流 以数据为基准 从手机存储往文件走为输出流
        try {
            //追加模式
            FileOutputStream fos_1 = openFileOutput(FILENAME,MODE_APPEND);

            //3.构造打印流 PrintStream
            PrintStream pm = new PrintStream(fos_1);

            //4.写入内容(换行)
            pm.println(content);

            //5.关闭
            pm.close();
            fos_1.close();

            Toast.makeText(Activitydata.this, "保存成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();

            Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
        }

    }

    //获取
    public void onclick4(View view)
    {
        //1.获取输入流  从文件到手机存储
        try {
            FileInputStream fis = openFileInput(FILENAME);

            //2.用数组方法读取
            //定义读取的数组
            byte[] b = new byte[1024];

            //3.读出的数据的长度
            int i=0;
            StringBuilder sbr = new StringBuilder();

            //fis.read(b)返回长度
            while((i=fis.read(b))>0)
            {
                //在这里需要字符串,转为字符串
                sbr.append(new String(b,0,i));
            }

            fis.close();

            //显示读出的内容
            et_4.setText(sbr);

            Toast.makeText(Activitydata.this, "读取成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {

            e.printStackTrace();

            Toast.makeText(Activitydata.this, "读取失败", Toast.LENGTH_SHORT).show();
        }

    }

    //操作assets内的文件
    public void onclick5(View view)
    {
        //1.获取AssetManager
        AssetManager assetManager = getAssets();

        //2.打开文件  返回输入流 把文件读到内存里
        try {
            InputStream is = assetManager.open("touxiang.jpg");

            //3.获取输出流
            FileOutputStream fos = openFileOutput("touxiang2.jpg",MODE_PRIVATE);

            //4.边读边写
            byte[] b =new byte[1024];

            int i = 0;

            while((i = is.read(b))>0)
            {
                fos.write(b, 0, i);
            }

            fos.close();
            is.close();

            Toast.makeText(Activitydata.this, "保存成功", Toast.LENGTH_SHORT).show();

        } catch (IOException e) {
            e.printStackTrace();

            Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
        }

    }

    //读取文件 从手机内部存储读图片文件
    public void onclick6(View view)
    {

        //改变ImageView的图片来源,指向手机存储空间

        //1.获取文件存储的绝对路径
        String filepath = getFilesDir().getAbsolutePath();

        //2.组合完整路径
        filepath += "/touxiang2.jpg";

        Toast.makeText(Activitydata.this, "path= "+filepath, Toast.LENGTH_SHORT).show();

        //3.生成位图实例
        Bitmap bm = BitmapFactory.decodeFile(filepath);

        //4.改变ImageView的图片来源
        iv_3.setImageBitmap(bm);

    }

 

posted on 2016-04-10 16:17  Chen_s  阅读(621)  评论(0编辑  收藏  举报

导航