javascript - 相机插件未在 Ionic 4 中打开相机
问题描述
我正在 Ionic 4 中开发一个项目。我希望用户通过从相机捕获或从库上传来上传图片。现在我正在开发模式下运行。问题是我无法在调试模式下打开设备上的相机或照片库。但是,当我在“DEVAPP”上运行时,相机正在打开。我尝试过获得许可,但没有任何效果。这是我的代码,尝试了很多方法,因此代码有点分散:
async selectImage() {
try {
const actionSheet = await this.actionSheetController.create({
header: 'Select Image source',
buttons: [{
text: 'Load from Library',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
await actionSheet.present();
} catch (error) {
alert(error);
}
}
takePicture(sourceType: PictureSourceType) {
const options: CameraOptions = {
quality: 100,
sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
alert('i m n takepicture');
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CAMERA]).then(
result => console.log('i am asking for permision: ' + result),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
);
if (this.camera.PictureSourceType.CAMERA) {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
result =>
this.camera.getPicture(options).then(imagePath => {
if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
const correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
const currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}). catch((error) => {
console.warn('error: ' + error);
});
} else {
const currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
const correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA));
} else {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.PHOTOLIBRARY).then(
result =>
this.camera.getPicture(options).then(imagePath => {
if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
const correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
const currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
const currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
const correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.PHOTOLIBRARY));
}
}
解决方案
尝试这个
takePicture(sourceType: PictureSourceType) {
const options: CameraOptions = {
quality: 100,
sourceType:sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
this.camera.getPicture(options).then(
imageData => {
///DO YOUR LOGIC
},
err => {
// Handle error
}
);
}
推荐阅读
- node.js - GAE 部署错误,tar_runtime_package 退出并出现错误 [Errno 12] 无法分配内存
- postgresql - Postgresql 最大 TransactionId > 40 亿
- python - pygame draw.rect 不适用于surface.fill
- math - 找到可以包含隐式定义函数的最小框
- autohotkey - 对三重组合键使用 GetKeyState 不起作用
- sql - 如何在sequelize中将外键引用到另一个外键?
- node.js - docker/nodejs/mongodb 部署:MongooseError [MongooseServerSelectionError]:连接超时
- django - Django RestFramework 弹性搜索:时间线 API
- javascript - 如何用鼠标选择范围单元格?
- node.js - 无法弄清楚如何返回 socket.join 真实性