Angular Ionic-使用特定文件名将图片保存到设备
我正在开发Ionic/Cordova应用程序。Angular Ionic-使用特定文件名将图片保存到设备,angular,cordova,ionic-framework,camera,Angular,Cordova,Ionic Framework,Camera,我正在开发Ionic/Cordova应用程序。 我想拍照并将其保存在我的设备上。我设法做到了,但我真的不知道如何给出一个特定的文件名(而不是“IMG_20200903…”文件名) 我该怎么办? 以下是片段: gallery(){ const options: CameraOptions = { quality: 20, destinationType: this.camera.DestinationType.FILE_URI, encodingType: this.
我想拍照并将其保存在我的设备上。我设法做到了,但我真的不知道如何给出一个特定的文件名(而不是“IMG_20200903…”文件名) 我该怎么办?
以下是片段:
gallery(){
const options: CameraOptions = {
quality: 20,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: true
}
this.camera.getPicture(options).then((imageData) => {
let base64Image = 'data:image/jpeg;base64,' + imageData;
this.clickedImagePath = imageData;
}, (err) => {
alert(err);
});
}
任何帮助都将不胜感激
//here injecting camera and file class to our component part as object
constructor(private camera: Camera, private file: File) {}
//here this method is used to start a camera and take a picture and save a picture in specific mentioned part.
public getPicture() {
let base64ImageData;
const options: CameraOptions = {
//here is the picture quality in range 0-100 default value 50. Optional field
quality: 100,
/**here is the format of an output file.
*destination type default is FILE_URI.
* DATA_URL: 0 (number) - base64-encoded string,
* FILE_URI: 1 (number)- Return image file URI,
* NATIVE_URI: 2 (number)- Return image native URI
*/
destinationType: this.camera.DestinationType.DATA_URL,
/**here is the returned image file format
*default format is JPEG
* JPEG:0 (number),
* PNG:1 (number),
*/
encodingType: this.camera.EncodingType.JPEG,
/** Only works when Picture Source Type is PHOTOLIBRARY or SAVEDPHOTOALBUM.
*PICTURE: 0 allow selection of still pictures only. (DEFAULT)
*VIDEO: 1 allow selection of video only.
*/
mediaType: this.camera.MediaType.PICTURE,
/**here set the source of the picture
*Default is CAMERA.
*PHOTOLIBRARY : 0,
*CAMERA : 1,
*SAVEDPHOTOALBUM : 2
*/
sourceType: this.camera.PictureSourceType.CAMERA
}
this.camera.getPicture(options).then((imageData) => {
//here converting a normal image data to base64 image data.
base64ImageData = 'data:image/jpeg;base64,' + imageData;
/**here passing three arguments to method
*Base64 Data
*Folder Name
*File Name
*/
this.writeFile(base64ImageData, “My Picture”, “sample.jpeg”);
}, (error) => {
console.log('Error Occured: ' + error);
});
}
//here is the method is used to write a file in storage
public writeFile(base64Data: any, folderName: string, fileName: any) {
let contentType = this.getContentType(base64Data);
let DataBlob = this.base64toBlob(base64Data, contentType);
// here iam mentioned this line this.file.externalRootDirectory is a native pre-defined file path storage. You can change a file path whatever pre-defined method.
let filePath = this.file.externalRootDirectory + folderName;
this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {
console.log("File Writed Successfully", success);
}).catch((err) => {
console.log("Error Occured While Writing File", err);
})
}
//here is the method is used to get content type of an bas64 data
public getContentType(base64Data: any) {
let block = base64Data.split(";");
let contentType = block[0].split(":")[1];
return contentType;
}
//here is the method is used to convert base64 data to blob data
public base64toBlob(b64Data, contentType) {
contentType = contentType || '';
sliceSize = 512;
let byteCharacters = atob(b64Data);
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
let blob = new Blob(byteArrays, {
type: contentType
});
return blob;
}
//这里将摄影机和文件类作为对象注入到我们的组件中
构造函数(专用摄影机:摄影机,专用文件:文件){}
//在这里,这种方法用于启动相机并拍摄照片,并将照片保存在所述的特定部分。
公共getPicture(){
让base64ImageData;
常量选项:CameraOptions={
//这是0-100范围内的图片质量默认值50。可选字段
质量:100,
/**以下是输出文件的格式。
*目标类型默认为文件\ URI。
*数据URL:0(数字)-base64编码字符串,
*文件URI:1(编号)-返回图像文件URI,
*本机URI:2(数字)-返回图像本机URI
*/
destinationType:this.camera.destinationType.DATA\u URL,
/**这是返回的图像文件格式
*默认格式为JPEG
*JPEG:0(数字),
*巴布亚新几内亚:1(数字),
*/
编码类型:this.camera.encodingType.JPEG,
/**仅当图片源类型为PHOTOLIBRARY或SAVEDPHOTOALBUM时有效。
*图片:0仅允许选择静态图片。(默认)
*视频:1仅允许选择视频。
*/
mediaType:this.camera.mediaType.PICTURE,
/**这里设置了图片的来源
*默认设置为照相机。
*照片库:0,
*摄像机:1,
*SAVEDPHOTOALBUM:2
*/
sourceType:this.camera.PictureSourceType.camera
}
this.camera.getPicture(选项)。然后((imageData)=>{
//这里将普通图像数据转换为base64图像数据。
base64图像数据='数据:图像/jpeg;base64'+图像数据;
/**这里将三个参数传递给方法
*Base64数据
*文件夹名
*文件名
*/
这个.writeFile(base64ImageData,“我的图片”,“sample.jpeg”);
},(错误)=>{
console.log('发生错误:'+错误);
});
}
//下面是用于在存储器中写入文件的方法
公共writeFile(base64Data:any,folderName:string,fileName:any){
让contentType=this.getContentType(base64Data);
让DataBlob=this.base64toBlob(base64Data,contentType);
//这里我提到了这一行this.file.externalRootDirectory是一个本地预定义的文件路径存储。您可以通过任何预定义的方法更改文件路径。
让filePath=this.file.externalRootDirectory+folderName;
this.file.writeFile(文件路径、文件名、数据块、内容类型)。然后((成功)=>{
log(“文件写入成功”,成功);
}).catch((err)=>{
console.log(“写入文件时出错”,err);
})
}
//下面是用于获取bas64数据的内容类型的方法
public getContentType(base64Data:any){
let block=base64Data.split(“;”);
让contentType=block[0]。拆分(“:”[1];
返回contentType;
}
//下面是用于将base64数据转换为blob数据的方法
public base64toBlob(b64Data,contentType){
contentType=contentType | |“”;
切片大小=512;
let byteCharacters=atob(b64Data);
let ByteArray=[];
对于(让offset=0;offset
希望这有帮助 好的,谢谢,但我猜
相机照片
与电容器(@capactor/core)有关。如何在没有电容器和摄像照片的情况下制作?太棒了!我试过了,但是在let-DataBlob=this.base64toBlob(content,contentType)行中有一个错误代码>。错误是找不到应为base64Data的名称内容
。感谢您的编辑。我尝试了以下代码:相机处于活动状态,拍摄了一张照片,但似乎没有下载到设备中(可能我不知道应该在哪里查找,但没有找到)。根据代码,检查控制台的成功状态(“文件写入成功”)。如果可以,请检查externalRootDirectory是否包含文件夹名“My Picture”,如果存在,则图像将保存在该文件夹中。
//here injecting camera and file class to our component part as object
constructor(private camera: Camera, private file: File) {}
//here this method is used to start a camera and take a picture and save a picture in specific mentioned part.
public getPicture() {
let base64ImageData;
const options: CameraOptions = {
//here is the picture quality in range 0-100 default value 50. Optional field
quality: 100,
/**here is the format of an output file.
*destination type default is FILE_URI.
* DATA_URL: 0 (number) - base64-encoded string,
* FILE_URI: 1 (number)- Return image file URI,
* NATIVE_URI: 2 (number)- Return image native URI
*/
destinationType: this.camera.DestinationType.DATA_URL,
/**here is the returned image file format
*default format is JPEG
* JPEG:0 (number),
* PNG:1 (number),
*/
encodingType: this.camera.EncodingType.JPEG,
/** Only works when Picture Source Type is PHOTOLIBRARY or SAVEDPHOTOALBUM.
*PICTURE: 0 allow selection of still pictures only. (DEFAULT)
*VIDEO: 1 allow selection of video only.
*/
mediaType: this.camera.MediaType.PICTURE,
/**here set the source of the picture
*Default is CAMERA.
*PHOTOLIBRARY : 0,
*CAMERA : 1,
*SAVEDPHOTOALBUM : 2
*/
sourceType: this.camera.PictureSourceType.CAMERA
}
this.camera.getPicture(options).then((imageData) => {
//here converting a normal image data to base64 image data.
base64ImageData = 'data:image/jpeg;base64,' + imageData;
/**here passing three arguments to method
*Base64 Data
*Folder Name
*File Name
*/
this.writeFile(base64ImageData, “My Picture”, “sample.jpeg”);
}, (error) => {
console.log('Error Occured: ' + error);
});
}
//here is the method is used to write a file in storage
public writeFile(base64Data: any, folderName: string, fileName: any) {
let contentType = this.getContentType(base64Data);
let DataBlob = this.base64toBlob(base64Data, contentType);
// here iam mentioned this line this.file.externalRootDirectory is a native pre-defined file path storage. You can change a file path whatever pre-defined method.
let filePath = this.file.externalRootDirectory + folderName;
this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {
console.log("File Writed Successfully", success);
}).catch((err) => {
console.log("Error Occured While Writing File", err);
})
}
//here is the method is used to get content type of an bas64 data
public getContentType(base64Data: any) {
let block = base64Data.split(";");
let contentType = block[0].split(":")[1];
return contentType;
}
//here is the method is used to convert base64 data to blob data
public base64toBlob(b64Data, contentType) {
contentType = contentType || '';
sliceSize = 512;
let byteCharacters = atob(b64Data);
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
let blob = new Blob(byteArrays, {
type: contentType
});
return blob;
}