0

i'm trying to build an app that notices when a file is created. I've used WatchService but when i attempt to "register" there is the following error:

W/System.err: java.nio.file.AccessDeniedException: /storage/emulated/0/Wow
W/System.err:     at sun.nio.fs.LinuxWatchService$Poller.implRegister(LinuxWatchService.java:275)
        at sun.nio.fs.AbstractPoller.processRequests(AbstractPoller.java:260)
        at sun.nio.fs.LinuxWatchService$Poller.run(LinuxWatchService.java:352)
        at java.lang.Thread.run(Thread.java:919)

I've also add all permissions in my manifest:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyWatchservice"
        android:requestLegacyExternalStorage="true">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MainClass"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />

            </intent-filter>
        </service>
    </application>

</manifest>

my code in MainClass.java

                WatchService service = FileSystems.getDefault().newWatchService();
                Path path = Paths.get(Environment.getExternalStorageDirectory().getPath()+"/Wow");
                Log.e("test",path.toString());
    
                File dir5 = new File(String.valueOf(path));
                if(dir5.exists())
                    Log.e("test1","ye");
                else
                    Log.e("test1","nope");
    
    
                WatchKey key;
                path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
                ...
SempriGno
  • 21
  • 3
  • Do you have "android:requestLegacyExternalStorage="true"" in your manifest? – David Lee Jul 01 '21 at 06:21
  • (@DavidLee: does the `application`-element from the second "code" block do? I see no `/>` (to end an empty element), no closing tag.) – greybeard Jul 01 '21 at 10:31
  • From *Android storage use cases and best practices*: [Caution: After you update your app to target Android 11 (API level 30), the system ignores the requestLegacyExternalStorage attribute when your app is running on Android 11 devices, so your app must be ready to support scoped storage and to migrate app data for users on those devices.](https://developer.android.com/training/data-storage/use-cases): what API level are you targeting? – greybeard Jul 01 '21 at 10:38
  • Does this answer your question: [opting into the legacy storage model](https://stackoverflow.com/a/58379655)? – greybeard Jul 01 '21 at 10:42
  • yes i have android:requestLegacyExternalStorage="true" in my manifest, i've edit the question with full manifest – SempriGno Jul 01 '21 at 11:00
  • @greyberad That's all you need in the `application` tag. – David Lee Jul 01 '21 at 11:01
  • (@DavidLee: I asked *element*, you answered *tag*. SempriGno commented `[I've edited] the question with full manifest` - I'm none the wiser. *I* have any neither manifest nor android?!) – greybeard Jul 01 '21 at 11:07
  • now it's updated, i can't put the entire manifests, too code.... – SempriGno Jul 01 '21 at 11:09
  • @greybeard Target Api: 26 – SempriGno Jul 02 '21 at 20:54
  • @DavidLee now it's updated – SempriGno Jul 02 '21 at 20:55

0 Answers0