简体   繁体   中英

Inserting captured image into database issue (Cordova/SQlite/JavaScript)

I am attempting to store captured images into my database however I cannot seem to get the actual data of the image.

Once the camera takes a photo it should be automatically stored, however I have the query alert, and it shows as the image being undefined.

I'm sure there are a few problems with my coding, as my coding isn't the best.

alert(_Query3); gives me the following:

INSERT INTO Gallery(myImage) values ('undefined')

JavaScript:

   document.addEventListener("deviceready", onDeviceReady, false);
          var db;
          function onDeviceReady() {
            db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
            db.transaction(function(tx) {
           tx.executeSql('CREATE TABLE IF NOT EXISTS Gallery (id INTEGER PRIMARY KEY, myImage BLOB)');
            }, errorE, successS);
          }

function successS() {
  alert("Camera database ready!");
  document.getElementById("btnCamera").onclick = function() {
    navigator.camera.getPicture(onSuccess, onFail, {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL
    });
  };
}

function onSuccess(tx, imageData) {
alert("Camera test 1");
  var image = document.getElementById("lastPhoto");
  image.src = "data:image/jpeg;base64," + imageData;
  base64imageData = imageData;
  var _Query3 = ("INSERT INTO Gallery(myImage) values ('"+ base64imageData +"')");
  alert(_Query3);
  tx.executeSql(_Query3);
}
            /*   function successCamera() {
               navigator.notification.alert("Image has been stored", null, "Information", "ok");
                 $( ":mobile-pagecontainer" ).pagecontainer( "change", "#page4" );
             } */

function onFail(message) {
  alert('Failed because: ' + message);
}

function errorE(err) {
  alert("Error processing SQL: " + err.code);
}

I might have found a solution to your problem. Your onSuccess Callback seems to be of the wrong syntax. I don't understand how the "tx" variable can be used with the imageData variable because only a single parameter can be used for the onSuccess as far as I know. If my assumptions are correct, then the Base64 encoding of the image data must be stored in the tx parameter.

Let me know if this has managed to solve your issue.

You can refer to camera.onSuccess for the proper declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM