android文件存储

来源:互联网 发布:笑傲江湖捏脸数据 编辑:程序博客网 时间:2024/05/04 18:43

android存储区域

android存储区域分为外部存储和内部存储,最早是根据硬件设备进行的这样的划分,现在是逻辑划分




内部存储


Internal内部存储是确保不被其他用户和其他app所访问的最佳存储区域

1、特点 :

(1)外部存储总是可用的

(2)内部存储的文件默认只能被相应app访问

2、内部存储保存文件和读取

getFile():返回一个文件路径,代表app的Internal目录,即data/data/包名/Files/
getCache():返回一个Cache文件路径,代表app的Internal缓存目录,即data/data/包名/Cache/

 /**     * 向内部存储区域写入文件     */    public void writeFileToInternal() {        FileOutputStream fileOutputStream = null;        try {            //android模式下的文件输出流            fileOutputStream = openFileOutput("writeToInternal", MODE_PRIVATE);            //fileOutputStream = new FileOutputStream(new File(getFilesDir(), "writeToInternal"));            fileOutputStream.write(mWriteInternalEdit.getText().toString().getBytes());            Toast.makeText(InternalActivity.this, "写入成功", Toast.LENGTH_LONG).show();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                assert fileOutputStream != null;                fileOutputStream.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public void readFromInternal() {        File file = new File(getFilesDir(), "writeToInternal");        try {            //android模式下的文件输入流            FileInputStream fis = openFileInput("writeToInternal");            //  FileInputStream fis = new FileInputStream(file);            byte[] b = new byte[1024];            int length = fis.read(b);            String str = new String(b, 0, length);            mShowMsgTxt.setText(str);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }

外部存储区域

保存文件至外部存储时需要在AndroidManifest中注册外部存储读写权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

外部存储时需检查外部存储是否可用
  public boolean isExternalStorageWritable() {        String state = Environment.getExternalStorageState();        if (state.equals(Environment.MEDIA_MOUNTED)) {            return true;//外部存储设备被挂载,外部存储可用        } else {            return false;        }    }


外部存储公共区域

1、特点:

其他用户和app是可以公用的,卸载这个app时,这部分资源会保留

2、外部存储保存文件和读取

Environment.getExternalStoragePublicDirectory() :返回一个文件路径,参数为null时,代表外部存储的根目录,即mnt/sdcard/
/**     * 向外部存储公共区域写入图片     */    public void writeToExternalPublic() {        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "ExternalImage.png");        try {            FileOutputStream fileOutputStream = new FileOutputStream(file);            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banana);            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);            fileOutputStream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }/**     * 向外部存储公共区域写入图片     */    public void readFromExternalPublic() {        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "ExternalImage.png");        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());        mReadExternalPublicImg.setImageBitmap(bitmap);    }}

外部存储私有区域

1、特点:

一个app所独有的,在app被卸载时这部分数据会被删除,这些资源对于其他app是没有意义的

2、外部存储私有区域保存文件和读取

getExternalFileDir():返回一个文件路径,当参数为null时,代表私有区域的根目录,即 mnt/sdcard/Android/data/包名/
0 0
原创粉丝点击