Android 应用开发中,文件存储是基础且重要的部分。以下是一些关于 Android 文件存储的文档和教程。

  • 存储类型

    • 内部存储:应用自身的存储空间,只能被应用访问。
    • 外部存储:公共存储空间,可以被所有应用访问。
  • 文件存储操作

    • 创建文件
      File file = new File(getFilesDir(), "example.txt");
      try {
          FileOutputStream fos = new FileOutputStream(file);
          fos.write("Hello, World!".getBytes());
          fos.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
      
    • 读取文件
      File file = new File(getFilesDir(), "example.txt");
      try {
          FileInputStream fis = new FileInputStream(file);
          byte[] bytes = new byte[(int) file.length()];
          fis.read(bytes);
          String content = new String(bytes);
          fis.close();
          Log.d("FileContent", content);
      } catch (IOException e) {
          e.printStackTrace();
      }
      
  • 图片存储示例

    • 保存图片到内部存储
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.example);
      File file = new File(getFilesDir(), "example.jpg");
      try {
          FileOutputStream fos = new FileOutputStream(file);
          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
          fos.flush();
          fos.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
      
  • 扩展阅读

Android 图标