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
- Android存储-文件存储
- android存储---文件存储
- Android-文件存储-文本存储
- Android 数据存储--文件存储
- android 数据存储-文件存储
- android 文件存储
- android 文件存储
- Android之文件存储
- android 文件存储
- Android文件存储
- android文件存储
- android文件存储
- Android 文件存储解决方案
- Android中的文件存储
- Android 存储 文件
- Android文件存储
- android文件存储
- android加密存储文件
- Roberts边缘检测算子
- ListView数据显示动画
- iOS 实现第三方登录(图文)
- java--数据结构--二叉树遍历
- 01-初识正则表达式
- android文件存储
- 面试题:关于TCP和UDP
- xpath的语法
- 重新认识View(三)
- 2049不容易系列之(4)——考新郎(递推)(错排+组合数)
- ui-router入门(精简)
- 一致性哈希
- 编写C语言版本的卷积神经网络CNN之一:前言与Minst数据集
- Andorid之taskAffinity 和 FLAG_ACTIVITY_NEW_TASK