You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 10中FileObserver新构造函数报错问题求助

Fixing FileObserver Issues on Android 10 (API 29)

Hey there, let's work through this problem together. That NoSuchMethodError is a tricky one, but the root cause and fix are straightforward once you know what's going on.

Why You're Seeing This Error

First, the FileObserver(File file) constructor added in API 29 is a hidden system API—it's not meant to be called directly by third-party apps. Even though it shows up in the SDK docs, the Android runtime blocks access to it, which is why you get that "no direct method" crash when trying to use it.

Step 1: Fix the Constructor Crash

Ditch the FileObserver(File file) constructor entirely. Instead, go back to the deprecated FileObserver(String path, int mask) constructor. Don't worry about the "deprecated" label here—this method is still fully functional for app use cases; the deprecation is just a heads-up about scoped storage changes (which we'll cover next).

Here's the corrected code:

// Get the absolute path of your target folder
String targetFolderPath = new File(Environment.getExternalStorageDirectory(), "YourTargetFolder").getAbsolutePath();

// Initialize FileObserver with the string path
FileObserver fileObserver = new FileObserver(targetFolderPath, FileObserver.CREATE) {
    @Override
    public void onEvent(int event, String path) {
        if (event == FileObserver.CREATE && path != null) {
            // Handle the new file creation event here
        }
    }
};

// Start watching
fileObserver.startWatching();

Step 2: Fix Android 10 Scoped Storage Restrictions

Even with the correct constructor, your observer might still fail on Android 10 because of Scoped Storage. Here's how to address this based on your target folder:

  • If you're watching your app's private storage:
    Use getExternalFilesDir() to access your app's dedicated folder (e.g., context.getExternalFilesDir(null)). No extra permissions are needed here, and FileObserver will work as expected.

  • If you're watching a public folder (like Downloads or Pictures):
    Option 1: Use ContentObserver with MediaStore to listen for file changes in public directories. This is the recommended approach for scoped storage compliance.
    Option 2: If your app is a file manager or requires broad storage access, request the MANAGE_EXTERNAL_STORAGE permission. Note that this is a special permission—users have to enable it manually in Settings, and Google has strict review requirements for apps using it.

Quick Recap

  1. Avoid the hidden FileObserver(File file) constructor to fix the NoSuchMethodError.
  2. Use the deprecated (but still valid) string-path constructor instead.
  3. Adjust your storage access to comply with Android 10's scoped storage rules, either by using app-private storage or switching to ContentObserver for public folders.

内容的提问来源于stack exchange,提问作者Francesco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:53:12