Cordova-plugin-camera:ref.putString不是函数

时间:2018-08-01 14:50:45

标签: javascript cordova

我在离子安卓应用程序中遇到错误。使用与我的ios应用程序完全相同的代码(具有两个平台的ionic:android和ios),我无法向base64 uri发送Firebase storage

错误是:

Uncaught TypeError: mountainImagesRef.putString is not a function at navigator.camera.getPicture.quality (controllers.js:202) at Object.callbackFromNative (cordova.js:294) at <anonymous>:1:9

我在这里看到了类似的东西,但是解决方案是更新插件,并且我已经在使用cordova-plugin-camera的最新版本:V 4.0.3。 Uncaught TypeError: ref.putString is not a function

在我的ios应用上,.putString()函数正在运行!如何在我的android应用程序上执行此操作?你看到我做错了什么吗?我已经尝试了多种使用putString之类的uri.substring(0,23)等方法。

 function takePic()
 {
 
  navigator.camera.getPicture(
    function(uri){

	$ionicPopup.alert({
	         title: 'A new image',
	         template: 'Loading !'
	});
	
	// Create a root reference
	var storageRef = firebase.storage().ref();

	var filename = Math.floor((Math.random()*10000)+ 1);
	var newAvatar_name = filename+".jpg";

	var mountainImagesRef = storageRef.child("avatars/"+newAvatar_name);

	mountainImagesRef.putString(uri, "base64").then(function(snapshot) {
		console.log("Uploaded a base64 string!");
	});

    },
    function(){
	   $ionicPopup.alert({
	         title: 'Error',
	         template: 'impossible to select one'
	         });
    },
    {

    	    quality: 60,
    	    targetHeight: 300,
    	    targetWidth: 300,
        	destinationType: 0,
        	sourceType: 1,
        	mediaType: 0,
        	saveToPhotoAlbum: false
     }
     );
}

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,而不是使用似乎是问题的putString(),而是将相机uri转换为blob文件,然后使用put()

function takePic()
 {
 
  navigator.camera.getPicture(
    function(uri){

	$ionicPopup.alert({
	         title: 'A new image',
	         template: 'Loading !'
	});
	
	// Create a root reference
	var storageRef = firebase.storage().ref();

	var filename = Math.floor((Math.random()*10000)+ 1);
	var newAvatar_name = filename+".jpg";

	var mountainImagesRef = storageRef.child("avatars/"+newAvatar_name);

// Convert the uri into a byteArray

var contentType = contentType || '';
    var sliceSize = sliceSize || 512;

    let byteCharacters = atob(uri);
    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);
        }

        let byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }


    var mountainImagesRef = storageRef.child("avatars/"+newAvatar);

// Use the byteArrays to get my Blob

    var blob = new Blob(byteArrays);

    mountainImagesRef.put(blob).then(function(snapshot) {
        console.log("Uploaded a base64 string!");
    });
    
  },
    function(){
	   $ionicPopup.alert({
	         title: 'Error',
	         template: 'impossible to select one'
	         });
    },
    {

    	    quality: 60,
    	    targetHeight: 300,
    	    targetWidth: 300,
        	destinationType: 0,
        	sourceType: 1,
        	mediaType: 0,
        	saveToPhotoAlbum: false
     }
     );
}