简体   繁体   中英

Android not requesting permission in ionic app

I am building app that allows people to post pictures and videos, mainly trying to learn about ionic, cordova, android, and the like, but for some reason whenever I try to open a file using the cordova File plugin, it doesn't ask the user permission to access storage, and the code fails and the user is stuck on a loading screen. The code is pretty simple, they take a video, it gets transcoded, then the transcoded file is uploaded to a firebase storage bucket. If I quit the app, go to settings->apps->my app->permissions and then manually turn on the storage permission, it works. The problem is, I need to ask the user for permission either at run time or on install, and it doesn't. Here is the code..

this.media.captureVideo().then(file => {
  this.editor.transcodeVideo({
    fileUri: file[0].fullPath,
    outputFileType: this.editor.OutputFileType.MPEG4,
    outputFileName: 'temp_test',
    saveToLibrary: false,
    height: 800,
    width: 800,
    maintainAspectRatio: false
  }).then(fileUri => {
    let pathIndex = fileUri.lastIndexOf('/'),
        finalPath = 'file://' + fileUri.substr(0, pathIndex), 
        name = fileUri.substr(pathIndex + 1, fileUri.length);
    this.file.readAsArrayBuffer(finalPath, name).then(file => {
        //upload
    }).catch(err => { console.log(err); });
  });
});

However, this only works if I open up the settings on my phone, go to apps, then my app, then permissions, then enable storage manually. Is there some way to request the permission in ionic? I have searched and searched and all the answers I can find are specific to camera, or work in Java, but ionic isn't java. Thank you.

Effectively cordova-plugin-media-capture uses cordova-plugin-file , so the READ_EXTERNAL_STORAGE permission must be programatically asked.

Ionic offers native support for Android Permissions :

this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
        .then(status => {
          if (status.hasPermission) {
            this.captureVideo();
          } else {
            this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
            .then(status =>{
              if(status.hasPermission) this.captureVideo();
            });
          }
        }

Hope it helps.

The only thing that worked for me was the Android Diagnostic plugin. Check this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM