我在返回uri的格式时遇到问题:
“ content://com.android.providers.media.documents/document/image%3A18112”
而不是:
“ / storage / emulated / 0 / WhatsApp / Media / WhatsApp动画Gifs / VID-20191026-WA0003.mp4”
这似乎只是从图库中选择图片时的问题。对于视频文件,它是正确的格式。我的插件版本是:
“ @ ionic-native / camera”:“ ^ 5.15.1”
“ cordova-plugin-camera”:“ ^ 4.1.0”
据我所知,这是最新版本。我正在测试三星银河S8。
我的代码在下面:
import { Injectable } from '@angular/core';
import { CameraOptions, Camera, MediaType } from '@ionic-native/camera/ngx';
import { CameraProviderResponse } from '../objects/cameraProviderResponse';
@Injectable()
export class CameraProvider {
constructor(public camera: Camera) {
}
openCamera(selectedMediaType: MediaType, allowedMediaType: MediaType): Promise<CameraProviderResponse> {
const options: CameraOptions = {
sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
destinationType: this.camera.DestinationType.FILE_URI,
mediaType: selectedMediaType
};
return this.camera.getPicture(options).then((mediaPath) => {
let re = /(?:\.([^.]+))?$/;
let fileExtension = re.exec(mediaPath)[0];
let mediaType;
if (fileExtension === '.jpeg' || fileExtension === '.jpg' || fileExtension === '.png' || fileExtension === '.gif' && (allowedMediaType === MediaType.ALLMEDIA || allowedMediaType === MediaType.PICTURE)) {
mediaType = MediaType.PICTURE;
}
else if (fileExtension === '.mp4' && (allowedMediaType === MediaType.ALLMEDIA || allowedMediaType === MediaType.PICTURE)) {
mediaType = MediaType.VIDEO;
}
else {
return this.openCameraFailed();
}
return {
success: true,
mediaPath: mediaPath,
mediaType: mediaType,
fileExtension: fileExtension
};
}, error => {
return this.openCameraFailed();
}).catch(error => {
console.log(error);
return this.openCameraFailed();
});
}
openCameraFailed(): CameraProviderResponse {
return {
success: false
};
}
}
如果您需要更多信息。请询问。
我目前这样修复:
import { Injectable } from '@angular/core';
import { CameraOptions, Camera, MediaType } from '@ionic-native/camera/ngx';
import { CameraProviderResponse } from '../objects/cameraProviderResponse';
import { FilePath } from '@ionic-native/file-path/ngx';
//bug: temp fix stack overflow post: https://stackoverflow.com/questions/58581038/ionic-camera-plugin-file-uri-returning-wrong-filepath?noredirect=1#comment103477183_58581038
@Injectable()
export class CameraProvider {
constructor(public camera: Camera, public filePath: FilePath) {
}
openCamera(selectedMediaType: MediaType, allowedMediaType: MediaType): Promise<CameraProviderResponse> {
const options: CameraOptions = {
sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
destinationType: this.camera.DestinationType.FILE_URI,
mediaType: selectedMediaType
};
return this.camera.getPicture(options).then((mediaPath) => {
let fileExtension = this.getFileExtension(mediaPath);
if(this.getMediaType(fileExtension) === null) {
return this.filePath.resolveNativePath(mediaPath)
.then(path => {
return this.getCameraProviderResponse(allowedMediaType, path);
})
.catch(err => {
console.log(err);
return this.openCameraFailed();
});
}
else {
return this.getCameraProviderResponse(allowedMediaType, mediaPath);
}
}, error => {
return this.openCameraFailed();
}).catch(error => {
console.log(error);
return this.openCameraFailed();
});
}
getCameraProviderResponse(allowedMediaType: MediaType, path:string) {
let fileExtension = this.getFileExtension(path);
let mediaType = this.getMediaType(fileExtension);
if(mediaType === null) {
return this.openCameraFailed();
}
return {
success: true,
mediaPath: path,
mediaType: mediaType,
fileExtension: fileExtension
};
}
//fix for android
getFileExtension(path: string) {
let re = /(?:\.([^.]+))?$/;
return re.exec(path)[0];
}
//fix for android
getMediaType(fileExtension: string) {
if (fileExtension === '.jpeg' || fileExtension === '.jpg' || fileExtension === '.png' || fileExtension === '.gif') {
return MediaType.PICTURE;
}
else if (fileExtension === '.mp4') {
return MediaType.VIDEO;
}
else return null;
}
openCameraFailed(): CameraProviderResponse {
return {
success: false
};
}
}
感觉有些古怪,但现在就可以了。请我花点时间仔细研究这个问题。如果我找到更好的解决方案,我也会在这里发布它!同时。如果其他人找到解决此问题的更好方法,请在此处发布!