Android not requesting permission in ionic app

寵の児 提交于 2019-12-11 06:18:39

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/46143238/android-not-requesting-permission-in-ionic-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!