如何在cordova相机中保存带有自定义名称的照片

时间:2017-04-24 08:22:04

标签: javascript angularjs cordova firebase ionic-framework

我想知道是否有办法用Cordova相机命名我拍摄的照片,如果我可以从相册中搜索,则搜索名称,例如photoName.jpg = mysearch

这是我用来拍照的服务。

        var _takePhotoOff = function(url) {
        return $q(function(resolve, reject) {
            var options = {
                quality: 50,
                destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.CAMERA,
                allowEdit: true,
                popoverOptions: CameraPopoverOptions,
                saveToPhotoAlbum: true
            };
            $cordovaCamera.getPicture(options).then(function(imageData) {
                var photo = {
                    url: url,
                    photo: imageData
                }
                resolve(photo);
            });

        });
    }

2 个答案:

答案 0 :(得分:0)

您无法从插件中指定图像的名称,但您会在imageData对象中找到包含文件名的图像信息,并使用cordova-plugin-file插件可以访问并重命名文件

答案 1 :(得分:0)

我就是这样做的

import { Injectable } from '@angular/core';
import { Camera } from '@ionic-native/camera';
import { FilePath } from '@ionic-native/file-path';
import { File } from '@ionic-native/file';

@Injectable()
export class CameraHandlerProvider {

  constructor(private camera: Camera, private filePath: FilePath, private file: File ) {

    }

    config = {
        quality: 100,
    destinationType: this.camera.DestinationType.FILE_URI,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE
    }

    getPic(fileName: string) {
        this.camera.getPicture(this.config).then((imageData) => {
            this.filePath.resolveNativePath(imageData)
            .then((path) => {
                console.log(imageData, path);
                let imagePath = path.substr(0, path.lastIndexOf("/") + 1);
                let imageName = path.substring(path.lastIndexOf("/") + 1, path.length);
                this.file.moveFile(imagePath, imageName, this.file.dataDirectory, fileName)
                .then(newFile => {
                    console.log(newFile);
                })
                .catch(err => {
                    console.error(err);
                })
            })
            .catch((err) => {
                console.error(err);
            })
        }).catch((err) => {
            console.error(err);
        });
    }
}