无法使用 fileURI 查看从相机拍摄的图像
unable to view image taken from camera using fileURI
我正在使用 angular 4/Ionic4.i 需要使用 file_URI 而不是 data_URL 将图像发送到数据库,因为数据 URL 是用 base64 编码的占用太多内存。最初我使用 base64,它完全可以正常工作,但我的前辈告诉我对 FILE_uri 做同样的事情,我努力了 2 天来显示图像并将其发送到 database.i 尝试规范化-URL,domsanitizer,web-view 但没有 work.check 如果 upload() 代码正确发送图像到数据库
<img [src]="image"/>
image:any;
take(){
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: false
}
this.camera.getPicture(options).then((imageData) => {
// this.success = 'getting image is successful';
this.image =imageData;
// this.imageURI = this.webview.convertF(imageData);
}, (err) => {
this.err = err;
});
}
upload(){
let url="https://staging-api.kofluence.com/influencer/v1/profile";
let postData=new FormData();
postData.append('file',this.image);
let data:Observable<any>=this.http.post(url,postData);
data.subscribe((result)=>{
console.log(result);
}
);
}
1.在 ionic
中显示来自 fileURI 的图像源
你试过 webview 了吗?
如果不是请尝试以下步骤,ionic 不会显示带有 file://.. 的图像。它应该是 localhost://.. something.
以便安装 webview 包并将 file:// URL 转换为与 Web View 插件中的本地 Web 服务器兼容的 URL。
ionic cordova plugin add cordova-plugin-ionic-webview
npm install @ionic-native/ionic-webview
用法:
import { WebView } from '@ionic-native/ionic-webview/ngx';
constructor(private webview: WebView) { }
this.camera.getPicture(options).then((imageData) => {
this.image =this.webview.convertFileSrc(imageData);
}, (err) => {
this.err = err;
});
2。将文件 blob 上传到服务器
首先您必须将文件转换为 blob,然后附加到 formData。
注意:请记住这里 this.image 是文件路径的字符串,它不是一个确切的文件。
上传:
我们必须将图像转换为 blob 文件,然后将其发送到服务器。
upload(){
let url="https://staging-api.kofluence.com/influencer/v1/profile";
const blobValue = this.dataURItoBlob(this.image);
let postData=new FormData();
postData.append('file',blobValue);
let data:Observable<any>=this.http.post(url,postData);
data.subscribe((result)=>{
console.log(result);
}
);
将 dataUri 转换为 blob
private dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
let byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
let arrayBuffer = new ArrayBuffer(byteString.length);
let _ia = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
_ia[i] = byteString.charCodeAt(i);
}
let dataView = new DataView(arrayBuffer);
let blob = new Blob([dataView], { type: mimeString });
return blob;
}
如果我们在使用此代码时遇到任何问题,请告诉我。
我正在使用 angular 4/Ionic4.i 需要使用 file_URI 而不是 data_URL 将图像发送到数据库,因为数据 URL 是用 base64 编码的占用太多内存。最初我使用 base64,它完全可以正常工作,但我的前辈告诉我对 FILE_uri 做同样的事情,我努力了 2 天来显示图像并将其发送到 database.i 尝试规范化-URL,domsanitizer,web-view 但没有 work.check 如果 upload() 代码正确发送图像到数据库
<img [src]="image"/>
image:any;
take(){
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: false
}
this.camera.getPicture(options).then((imageData) => {
// this.success = 'getting image is successful';
this.image =imageData;
// this.imageURI = this.webview.convertF(imageData);
}, (err) => {
this.err = err;
});
}
upload(){
let url="https://staging-api.kofluence.com/influencer/v1/profile";
let postData=new FormData();
postData.append('file',this.image);
let data:Observable<any>=this.http.post(url,postData);
data.subscribe((result)=>{
console.log(result);
}
);
}
1.在 ionic
中显示来自 fileURI 的图像源你试过 webview 了吗?
如果不是请尝试以下步骤,ionic 不会显示带有 file://.. 的图像。它应该是 localhost://.. something.
以便安装 webview 包并将 file:// URL 转换为与 Web View 插件中的本地 Web 服务器兼容的 URL。
ionic cordova plugin add cordova-plugin-ionic-webview
npm install @ionic-native/ionic-webview
用法:
import { WebView } from '@ionic-native/ionic-webview/ngx';
constructor(private webview: WebView) { }
this.camera.getPicture(options).then((imageData) => {
this.image =this.webview.convertFileSrc(imageData);
}, (err) => {
this.err = err;
});
2。将文件 blob 上传到服务器
首先您必须将文件转换为 blob,然后附加到 formData。
注意:请记住这里 this.image 是文件路径的字符串,它不是一个确切的文件。
上传:
我们必须将图像转换为 blob 文件,然后将其发送到服务器。
upload(){
let url="https://staging-api.kofluence.com/influencer/v1/profile";
const blobValue = this.dataURItoBlob(this.image);
let postData=new FormData();
postData.append('file',blobValue);
let data:Observable<any>=this.http.post(url,postData);
data.subscribe((result)=>{
console.log(result);
}
);
将 dataUri 转换为 blob
private dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
let byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
let arrayBuffer = new ArrayBuffer(byteString.length);
let _ia = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
_ia[i] = byteString.charCodeAt(i);
}
let dataView = new DataView(arrayBuffer);
let blob = new Blob([dataView], { type: mimeString });
return blob;
}
如果我们在使用此代码时遇到任何问题,请告诉我。