I attempted to access a file within my emulator using the readFile
function of Capacitor filesystem. The file I am trying to manipulate is located in the Download folder of the emulator, and upon execution, the function returned the following error:
Error: File does not exist
Furthermore, I also tried utilizing the copy function to move this file to a directory where I could work with it. However, this action resulted in the following error:
Error: The source object does not exist
After performing permission tests within the application, I received a status of granted
.
Despite my efforts to resolve these issues by researching online and tweaking the parameters of Directory
in my filesystem service functions, all attempts still led to the same errors. It's worth noting that accessing the filesystem was successful on Android 10 with identical parameters.
All tests were conducted on an emulator running Pixel 4 with Android 11 (API30).
In the android.xml file, under the application
tag, I included
android:requestLegacyExternalStorage="true"
, along with the following 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 + '/test').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;
});
}
I am trying to ascertain the feasibility of accessing files outside of the app’s scoped folder on Android 11. My application necessitates importing files that may reside anywhere on the device as chosen by the user.
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
Installed Dependencies:
@capacitor/ios: not installed
@capacitor/cli: 3.0.0
@capacitor/android: 3.0.0
@capacitor/core: 3.0.0
EDIT
Attempts to adjust the target SDK to versions 28 and 29 did not alleviate the error.
According to documentation, adding the tag MANAGE_EXTERNAL_STORAGE
is necessary when attempting to access files beyond scoped storage. Despite already having this parameter in my manifest, I remain unable to read external content.
While examining filesystem permissions using both checkPermissions()
and requestPermissions()
, both functions indicate publicStorage: 'granted'.