【问题标题】:Cordova-plugin-camera : ref.putString is not a functionCordova-plugin-camera : ref.putString 不是函数
【发布时间】:2018-08-01 14:50:44
【问题描述】:

我的 ionic android 应用程序出现错误。使用与我的 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
     }
     );
}

【问题讨论】:

  • 在这里查看我的答案=> stackoverflow.com/a/51471159/1081909
  • 感谢您的回复,但我已经尝试改变我使用putString() 的方式,但问题似乎是putString() 未知...另外,用你的方法我收到此错误:window.resolveLocalFileSystemURL is not a function

标签: javascript cordova


【解决方案1】:

我终于找到了一个解决方案,而不是使用 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
     }
     );
}

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2018-06-22
    • 2017-03-03
    • 1970-01-01
    • 2018-06-18
    • 2017-08-04
    • 2018-04-27
    • 2022-11-07
    • 2016-10-06
    相关资源
    最近更新 更多