2017-01-31 117 views
0

我试图用NativeScript的相机模块拍摄照片,然后将其上传到Firebase,但似乎不起作用。 imageTakenfileLocationfalseundefined。我的代码有问题吗? (用TypeScript写)从NativeScript将图片上传到Firebase

import fs = require('file-system') 
import frame = require('ui/frame') 
import utils = require('utils/utils') 
import observableModule = require('data/observable') 
import imageSource = require("image-source") 
import camera = require('camera') 
import image = require('ui/image') 
import { 
    ImageFormat 
} from 'ui/enums' 
import view = require("ui/core/view") 
import firebase = require('nativescript-plugin-firebase') 
var dialog = require('nativescript-dialog') 
var pd = new observableModule.Observable() 

var imageContainer 
var imageTaken = false 
var fileLocation 

exports.loaded = args => { 
    var page = args.object 
    imageContainer = view.getViewById(page, "img") 

    pd.set('imageTaken', imageTaken) 
    page.bindingContext = pd 
} 

exports.takePhoto = args => { 
    const options = { 
    width: 300, 
    height: 300, 
    keepAspectRatio: true 
    } 
    camera.takePicture().then((picture) => { 
    console.log('Take Picture') 
    var image = new image.Image() 
    image.imageSource = picture 
    imageContainer.imageSource = picture 
    let savePath = fs.knownFolders.documents().path; 
    let fileName = 'img_' + new Date().getTime() + '_' + this.currentUserId.getValue() + '.' + ImageFormat.jpeg 
    let filePath = fs.path.join(savePath, fileName) 
    console.log(filePath) 
    picture.saveToFile(filePath, ImageFormat.jpeg) 
    fileLocation = filePath 
    imageTaken = true 

    }) 
} 

exports.sendPhoto = args => { 
    console.log(imageTaken) 
    console.log(fileLocation) 
    imageTaken ? upload(Math.random() + '-' + Date.now()) : dialog.show({ 
    title: "Error", 
    message: "Please take a photo first.", 
    okButtonText: "OK" 
    }) 
} 

const upload = (remoteFileName) => { 
    firebase.uploadFile({ 
    remoteFullPath: 'uploads/images/' + remoteFileName, 
    localFile: fs.File.fromPath(fileLocation), 
    localFullPath: fileLocation, 
    onProgress: function (status) { 
     console.log("Uploaded fraction: " + status.fractionCompleted) 
     console.log("Percentage complete: " + status.percentageCompleted) 
    } 
    }).then(
    uploadedFile => { 
     console.log("File uploaded: " + JSON.stringify(uploadedFile)) 
    }, 
    error => { 
     console.log("File upload error: " + error) 
    } 
) 
} 

回答

1

旧相机模块已过时。改为使用nativescript-camera。请注意,对于Android API23 +,您需要明确请求权限运行时。随着nativescript相机是大约nativescript相机插件here

+0

import * as camera from "nativescript-camera"; camera.requestPermissions(); 

完成更多的工作,最后,这是什么解决它。谢谢!只是问,你知道为什么他们仍然有相机在文档中,如果它的过时? – hashtagmemes

+0

从昨天开始,相机文章更新了这一个https://docs.nativescript.org/hardware/camera .. NativeScript文档每次发布都会更新,这可能是为什么旧文章处于活动状态的原因昨天(当新版本被宣布) –