Android IONIC camera插件文件_URI根据所选文件类型(视频/图片)返回不同的文件路径格式
返回的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插件照相机”:“^4.1.0” 据我所知,这是最新的版本。 我正在测试三星galaxy S8 我的代码如下:Android IONIC camera插件文件_URI根据所选文件类型(视频/图片)返回不同的文件路径格式,android,ionic-framework,camera,android-camera,cordova-plugins,Android,Ionic Framework,Camera,Android Camera,Cordova Plugins,返回的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” “cord
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
};
}
}
从'@angular/core'导入{Injectable};
从'@ionic native/Camera/ngx'导入{CameraOptions,Camera,MediaType};
从“../objects/CameraProviderResponse”导入{CameraProviderResponse};
@可注射()
出口级摄像机提供器{
建造商(公共摄像机:摄像机){
}
openCamera(selectedMediaType:MediaType,allowedMediaType:MediaType):承诺{
常量选项:摄影机选项={
sourceType:this.camera.PictureSourceType.SAVEDPHOTOALBUM,
destinationType:this.camera.destinationType.FILE\u URI,
mediaType:selectedMediaType
};
返回此.camera.getPicture(选项)。然后((mediaPath)=>{
设re=/(?:\([^.]+)?$/;
让fileExtension=re.exec(mediaPath)[0];
让媒体类型;
如果(fileExtension=='.jpeg'| | fileExtension=='.jpg'| | fileExtension=='.png'| | fileExtension=='.gif'&(allowedMediaType==MediaType.ALLMEDIA | allowedMediaType==MediaType.PICTURE)){
mediaType=mediaType.PICTURE;
}
else if(文件扩展名=='.mp4'&&(allowedMediaType===MediaType.ALLMEDIA | | allowedMediaType===MediaType.PICTURE)){
mediaType=mediaType.VIDEO;
}
否则{
返回此参数。openCameraFailed();
}
返回{
成功:没错,
mediaPath:mediaPath,
mediaType:mediaType,
fileExtension:fileExtension
};
},错误=>{
返回此参数。openCameraFailed();
}).catch(错误=>{
console.log(错误);
返回此参数。openCameraFailed();
});
}
openCameraFiled():CameraProviderResponse{
返回{
成功:错
};
}
}
如果你需要更多的信息。请询问。我目前是这样修复的:
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
};
}
}
从'@angular/core'导入{Injectable};
从'@ionic native/Camera/ngx'导入{CameraOptions,Camera,MediaType};
从“../objects/CameraProviderResponse”导入{CameraProviderResponse};
从'@ionic native/file path/ngx'导入{FilePath};
//错误:临时修复堆栈溢出帖子:https://stackoverflow.com/questions/58581038/ionic-camera-plugin-file-uri-returning-wrong-filepath?noredirect=1#comment103477183_58581038
@可注射()
出口级摄像机提供器{
构造函数(公共摄影机:摄影机,公共文件路径:文件路径){
}
openCamera(selectedMediaType:MediaType,allowedMediaType:MediaType):承诺{
常量选项:摄影机选项={
sourceType:this.camera.PictureSourceType.SAVEDPHOTOALBUM,
destinationType:this.camera.destinationType.FILE\u URI,
mediaType:selectedMediaType
};
返回this.camera.getPicture(选项)。然后((mediaPath)=>{
让fileExtension=this.getFileExtension(mediaPath);
if(this.getMediaType(文件扩展)==null){
返回此.filePath.resolveNativePath(mediaPath)
。然后(路径=>{
返回此.getCameraProviderResponse(allowedMediaType,路径);
})
.catch(错误=>{
控制台日志(err);
返回此参数。openCameraFailed();
});
}
否则{
返回此.getCameraProviderResponse(allowedMediaType,mediaPath);
}
},错误=>{
返回此参数。openCameraFailed();
}).catch(错误=>{
console.log(错误);
返回此参数。openCameraFailed();
});
}
getCameraProviderResponse(allowedMediaType:MediaType,路径:字符串){
让fileExtension=this.getFileExtension(path);
让mediaType=this.getMediaType(文件扩展名);
如果(mediaType==null){
返回此参数。openCameraFailed();
}
返回{
成功:没错,
mediaPath:path,
mediaType:mediaType,
fileExtension:fileExtension
};
}
//android修复程序
getFileExtension(路径:字符串){
设re=/(?:\([^.]+)?$/;
返回re.exec(路径)[0];
}
//android修复程序
getMediaType(文件扩展名:字符串){
如果(文件扩展名=='.jpeg'| |文件扩展名=='.jpg'| |文件扩展名==='.png'| |文件扩展名===='.gif'){
返回MediaType.PICTURE;
}
else if(fileExtension=='.mp4'){
返回MediaType.VIDEO;
}
否则返回null;
}
openCameraFiled():CameraProviderResponse{
返回{
成功:错
};
}
}
这感觉有点不舒服,但它现在起作用了。给我一些时间来仔细看看这个问题。如果我找到了更好的解决方案,我也会把它贴在这里!与此同时。如果其他人找到了更好的解决方案,请在这里发布 “我有一个问题,返回的uri的格式是”-为什么这是一个问题?获取
内容
Uri
是完全正常的。你也无法访问文件系统路径,比如你在Android 10(默认情况下)和Android R+(适用于所有应用程序)上引用的路径。好的,那么我如何才能选择图片的文件扩展名?为什么视频和GIF有效而图像无效?“那么我如何才能选择图片的文件扩展名?”——没有文件扩展名。As wi