![](/img/trans.png)
[英]Cordova Plugins Photo Library Plugin: Get downloaded picture file_uri path
[英]IONIC camera plugin FILE_URI returning different file path format based on selected file type (video/picture)
我有一個問題,返回的 uri 格式為:
“內容://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”
據我所知,哪些是最新版本。 我正在三星 Galaxy 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
};
}
}
感覺有點hacky,但現在可以解決問題。 給我一些時間來仔細研究這個問題。 如果我找到更好的解決方案,我也會在這里發布。 同時,如果其他人找到了更好的解決此問題的方法,請在此處發布!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.