Capacitor Filesystem error when I try to read a file in Android 11

I tried to read a file in my emulator using the function readFile of capacitor filesystem. The file that I trying manipulate it’s in emulator’s Download folder, and the function return this error:

Error: File does not exist

I also tried to use the copy fucntion, to copy this file to a directory where I can manipulate it, but this function returns me this error:

Error: The source object does not exist

Also I made the tests of permissions in the application, where I got granted.

I searched for this errors online, but without success. Also I used all the parameters of Directory on the functions of my filesystem service, but all the options returns me the same errors. With the same parameters it was possible access filesystem in android 10.

All the tests are make in an emulator Pixel 4 with Android 11 (API30).

In the android.xml, inside the tag application, I added android:requestLegacyExternalStorage="true", and in user-permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

homePage.ts

with copy

// file_dir_path = file:///storage/emulated/0/Download/
// file_name = file.kml
// path = 'MyApp/project' + this.projectId + '/temp'
return this.filesystem.copyFileToManegeDirectory(file_dir_path + file_name, path + '/teste').then(fileAs64 => {
	console.log(fileAs64).
});

with read

// file_dir_path = file:///storage/emulated/0/Download/
// file_name = file.kml
return readFile(file_dir_path + file_name).then(fileAs64 => {
    console.log(fileAs64).
});

FilesystemService

/** Copy Files */
// fileToCopy = file:///storage/emulated/0/Download/file.kml
copyFileToManegeDirectory(fileToCopy: string, directoryToGo: string): Promise<any> {
	return Filesystem.checkPermissions().then(permission => {
		console.log(permission)
		return Filesystem.copy({
			from: fileToCopy,
			directory: Directory.ExternalStorage,
			to: directoryToGo,
			toDirectory: Directory.External
		}).then(result => {
			console.log(result);
		})
	});
}

/** Read a file and returns a string base 64 */
readFile(path: string): Promise<string> {
	return Filesystem.readFile({
		path: path
	}).then(result => {
		return result.data;
	});
}

Ionic info


   Ionic CLI                     : 6.15.1-testing.0 (C:\Users\Gabriel Vieira\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.6.8
   @angular-devkit/build-angular : 12.0.2
   @angular-devkit/schematics    : 12.0.2
   @angular/cli                  : 12.0.2
   @ionic/angular-toolkit        : 4.0.0

Capacitor:

   Capacitor CLI      : 3.0.0
   @capacitor/android : 3.0.0
   @capacitor/core    : 3.0.0
   @capacitor/ios     : not installed

Utility:

   cordova-res : 0.15.3
   native-run  : 1.4.0

System:

   NodeJS : v14.16.1 (C:\Program Files (x86)\nodejs\node.exe)
   npm    : 6.14.12
   OS     : Windows 10

npx cap doctor

Latest Dependencies:

  @capacitor/cli: 3.0.1
  @capacitor/core: 3.0.1
  @capacitor/android: 3.0.1
  @capacitor/ios: 3.0.1

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 3.0.0
  @capacitor/android: 3.0.0
  @capacitor/core: 3.0.0
2 Likes

Public folders such as Documents and ExternalStorage are no longer available on Android 11+, and on Android 10 only if you enable the legacy storage, as you did.

3 Likes

Ok, how is it possible then to get files out of the app’s scoped folder on Android 11? My app needs to import files that can exist anywhere on the device (the user chooses where from).

3 Likes

I’m having the same problem. Can’t figure it out how to read files in other folders with Android 11, even after add permissions in manifest and programmatically acquiring user permission. My objective is also obtain external files to use in my app.

2 Likes

same thing, looking at documention and no awnsers yet.

1 Like

EDIT

Tried to change target SDK to 28 and 29, but the same error ocurred.

1 Like

I fixed changed the targetSdkVersion in android/variables.gradle from 30 to 29. I couldn’t fixed before because I was changing it in build.gradle. I also removed the tag <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> from the AndroidManifest

1 Like

This solution does not work for a production release, API level required according to documentation is currently 29, and should be 30 from November 2021, check the following link 符合 Google Play 的目标 API 级别要求  |  Android Developers
Let me know if you had found another solution since

2 Likes