androidtypescriptionic-frameworkionic6

Read Text File on Ionic - Android 12


Good evening, the problem that I can't solve concerns reading text files in Ionic 6 and Android 12 (method readAsText). Using the FilePath, FileChooser and File libraries, the results exist but I can't extract the text (null is returned). Has anyone faced a problem like this before?

The following code worked until a couple of months ago:

//PROPERTY
import { Injectable } from '@angular/core';
import { File } from '@ionic-native/file/ngx';
import { Device } from '@awesome-cordova-plugins/device/ngx';
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { FilePath } from '@awesome-cordova-plugins/file-path/ngx';
import { Platform } from '@ionic/angular';
import { HttpService } from './http.service';
import { Chooser } from '@awesome-cordova-plugins/chooser/ngx';


//CONSTRUCTOR
public file: File,
public device: Device,
private platform: Platform,
public fileChooser: FileChooser,
private chooser: Chooser,
public http: HttpService,
private filePath: FilePath 

....


//SNIPPET WITH ERROR
this.fileChooser
          .open()
          .then((uri) => {
            this.filePath
              .resolveNativePath(uri)
              .then((url) => {
                this.file
                  .resolveLocalFilesystemUrl(url)
                  .then((fileEntry: any) => {
                    this.platform.ready().then(() => {
                      debugger;
                      this.file.checkFile(fileEntry['nativeURL'].replace(fileEntry['name'], ''), fileEntry['name']).then(response => {
                        debugger;
                        if (response === true) {
                          this.file
                            .readAsText(
                              fileEntry['nativeURL'].replace(fileEntry['name'], ''),
                              fileEntry['name']
                            )
                            .then((result) => {
                              if (result) {
                           ---->     //RESULT ONLY NULL <---------
                                debugger;
                                resolve(result);
                              } else {
                                reject('Errore');
                              }
                            })
                            .catch((err) => {
                              console.log('err-->' + JSON.stringify(err));
                            });
                        }
                      }).catch(err => {
                        debugger;
                      });
                    });
                  });
              })
              .catch((err_3) => {
                reject(err_3);
              });
          })
          .catch((err_2) => {
            reject(err_2);
          });

Solution

  • I write what I did in case anyone needs. To allow the system to read files other than images, videos and audios, it is necessary to use the Android Storage Access Framework (SAF). On Ionic it integrates with the cordova-plugin-saf-mediastore plugin. I hope it will be useful to you, see you soon