【代码】Android|判断asserts下的文件存在与否,以及普通文件存在与否

asserts

复制代码
  boolean isAssertFileExists(String filename) {
      AssetManager assetManager = getActivity().getAssets();
      try {
          String[] files = assetManager.list("");
          for (String file : files) {
              if (file.equals(filename)) {
                  return true;
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      return false;
  }

需要引入的头文件:

复制代码
import java.io.IOException;
import android.content.res.AssetManager;

可以看到能正常判断:

其他外部文件或者data下的文件

如果是常规文件只需要用new File(filepath).exists()就行了。写成函数如下:

复制代码
  boolean checkFileExists(String dirname, String filename) {
    File file = new File(dirname, filename);
    return file.exists();
  }
相关推荐
Lesile23 分钟前
Android:Hilt框架入门 · 在ViewUI和ComposeUI下的应用
android·android jetpack
用户4238162290734 分钟前
Android 16 WebView 页面顶部挖孔/通知栏不能显示UI问题排查
android
weixin_727535621 小时前
Loop 已死,Graph 新生:AI 工作流的范式革命
android·人工智能·rxjava
严同学正在努力2 小时前
从备份到恢复:我用 30 分钟恢复了误删的核心业务表
android·java·数据库·ai
梦想三三3 小时前
LangChain Output Parser 实战:从字符串到结构化数据的完整指南
android·服务器·langchain·github·uv
爱笑鱼5 小时前
Binder(八):远端进程死了,BinderProxy 为什么还能收到通知?
android
Android-Flutter5 小时前
Kotlin 冷流与热流详解
android·kotlin
zhangphil5 小时前
Android ContentProvider/ContentResolver读图片,跨进程 IPC慢
android
yueqc16 小时前
Android .so 文件压缩
android·so·包体积
龚礼鹏6 小时前
深入解析 Android Automotive (AAOS) 启动流程与 CarService 核心机制(基于 Android 16 最新源码视角)
android