Android未在离子应用中请求权限 [英] Android not requesting permission in ionic app
问题描述
我正在构建允许人们发布图片和视频的应用程序,主要是尝试了解离子,cordova,android等等,但出于某种原因,每当我尝试使用cordova文件插件打开文件时,它都没有不要求用户访问存储的权限,代码失败,用户卡在加载屏幕上。
代码非常简单,他们拍摄视频,进行转码,然后将转码后的文件上传到firebase存储桶。如果我退出应用程序,请转到设置 - >应用程序 - >我的应用程序 - >权限,然后手动打开存储权限,它可以正常工作。问题是,我需要在运行时或安装时询问用户权限,但事实并非如此。这是代码..
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); });
});
});
但是,这只适用于我打开手机上的设置,转到应用程序,然后我的应用程序,然后是权限,然后手动启用存储。有没有办法在离子中申请许可?我搜索和搜索过,我能找到的所有答案都是针对相机的,或者是用Java工作的,但离子不是java。谢谢。
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.
推荐答案
有效 cordova-plugin-media-capture 使用 cordova-plugin-file ,所以 READ_EXTERNAL_STORAGE 权限。
Ionic为 Android权限提供原生支持:
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();
});
}
}
希望有所帮助。
这篇关于Android未在离子应用中请求权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!