Android——数据存储:手机外部存储 SD卡存储

xml

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_5"
        android:hint="要存储的的内容"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_6"
        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="onclick7"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="从带包名的目录中获取"
            android:layout_weight="1"
            android:onClick="onclick8"/>
    </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="onclick9"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="从自定义的目录中获取"
            android:layout_weight="1"
            android:onClick="onclick10"/>
    </LinearLayout>

java

需要权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   //向外部空间存储文件 保存到带包名的目录
    public void onclick7(View view)
    {
        //
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1.获取要存储的内容
            String content = et_5.getText().toString();

            //2.获取外部存储带包名的目录 ——只到根目录
           // String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //参数 代表不同文件类型的子目录 如果没有就传null 获取到根目录下的带包名的目录
            String sdpath = getExternalFilesDir(null).getAbsolutePath();
            Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //3.构造输出流
            sdpath += "/" +FILENAME;

            try {
                FileOutputStream fos = new FileOutputStream(sdpath,true);

                //传统方式  字节数组方式
                fos.write(content.getBytes("utf-8"));

                fos.close();

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


            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Toast.makeText(Activitydata.this, "sd卡没有挂载", Toast.LENGTH_SHORT).show();
        }

    }

    //从外部存储空间中读取带包名文件
    //判断是否挂载
    public void onclick8(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1.获取要存储的内容
            //String content = et_5.getText().toString();

            //2.获取外部存储带包名的目录 ——只到根目录
            // String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //参数 代表不同文件类型的子目录 如果没有就传null 获取到根目录下的带包名的目录
            String sdpath = getExternalFilesDir(null).getAbsolutePath();
           // Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //3.构造输入流
            sdpath += "/" +FILENAME;

            try {
                FileInputStream fis = new FileInputStream(sdpath);

                byte[] b = new byte[1024];

                int i = 0;

                StringBuilder sbr = new StringBuilder();

                while ((i = fis.read(b))>0)
                {
                    sbr.append(new String(b,0,i));
                    et_6.setText(sbr);
                }
                fis.close();

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


            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(Activitydata.this, "获取失败", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Toast.makeText(Activitydata.this, "sd卡没有挂载", Toast.LENGTH_SHORT).show();
        }
    }

    //保存文件到自定义目录
    public void onclick9(View view)
    {

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //1.获取要存储的内容
            String content = et_5.getText().toString();

            //2.获取外部存储的根目录
            String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();

            //在sd卡的根目录下创建子目录
            sdpath += "/hanqi";

            //实例化File,指向一个目录
            File file = new File(sdpath);

            //如果不存在创建一个
            if (!file.exists()) {
                //创建目录
                file.mkdirs();
            }
                Toast.makeText(Activitydata.this, "path= " + sdpath, Toast.LENGTH_SHORT).show();


                //3.创建输出流
                sdpath += "/" + FILENAME;

                try {
                    FileOutputStream fos = new FileOutputStream(sdpath, true);

                    //传统方式  字节数组方式
                    fos.write(content.getBytes("utf-8"));

                    fos.close();

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


                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(Activitydata.this, "保存失败", Toast.LENGTH_SHORT).show();
                }
            }

        else
        {
            Toast.makeText(Activitydata.this, "sd卡没有挂载", Toast.LENGTH_SHORT).show();
        }

    }

    public void onclick10(View view){

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1.获取要存储的内容
            //String content = et_5.getText().toString();

            //2.获取外部存储带包名的目录 ——只到根目录
            String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //参数 代表不同文件类型的子目录 如果没有就传null 获取到根目录下的带包名的目录
            //String sdpath = getExternalFilesDir(null).getAbsolutePath();
            // Toast.makeText(Activitydata.this, "sdpath= "+sdpath, Toast.LENGTH_LONG).show();

            //3.构造输入流
            sdpath += "/hanqi/" +FILENAME;

            Toast.makeText(Activitydata.this, ""+sdpath, Toast.LENGTH_SHORT).show();

            try {
                FileInputStream fis = new FileInputStream(sdpath);

                byte[] b = new byte[1024];

                int i = 0;

                StringBuilder sbr = new StringBuilder();

                while ((i = fis.read(b))>0)
                {
                    sbr.append(new String(b,0,i));
                    et_6.setText(sbr);
                }
                fis.close();

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


            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(Activitydata.this, "获取失败", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Toast.makeText(Activitydata.this, "sd卡没有挂载", Toast.LENGTH_SHORT).show();
        }

    }

原文地址:https://www.cnblogs.com/Chenshuai7/p/5381219.html