簡體   English   中英

如何使用Angular JS從SQLite DataBae獲取和顯示圖像

[英]how to fetch and display image from sqlite databae using angular js

我通過將圖像轉換為base64格式將圖像存儲在sqlite數據庫中並插入到DB。由於在插入記錄后獲得插入ID,所以成功插入了圖像,但是當我打開數據庫時,數據庫的表沒有任何列。
我的第二個問題是我想獲取圖像並將其顯示在特定區域,即pictureUrl(此處),但無法執行此操作。 下面是我的代碼。 建議所需的更改。 提前致謝 :)
app.js

var db=null;
var example=angular.module('starter', ['ionic', 'ngCordova']).run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
      if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }
    });
  db = window.openDatabase("my.db","1.0","my.db",100000);
  $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS imagewala (img blob)");
  window.alert("Database Created .. !")
});

  example.controller('CameraCtrl',function($scope, $cordovaCamera,$cordovaSQLite){
    function dataURItoBlob(dataURI, callback) {
      var byteString = atob(dataURI.split(',')[1]);
     var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
      var ab = new ArrayBuffer(byteString.length);
      var ia = new Uint8Array(ab);
      for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
      }
      var bb = new BlobBuilder();
      bb.append(ab);
      return bb.getBlob(mimeString);
    }
  $scope.pictureUrl="http://placehold.it/50x50";
    $scope.takePicture=function(img){
    var options = {
      destinationType: Camera.DestinationType.DATA_URL,
      encodingType: Camera.EncodingType.JPEG
      };
    $cordovaCamera.getPicture(options).then(function(imageData) {
     $scope.pictureUrl = "data:image/jpeg;base64," + imageData;
      var query = "INSERT INTO imagewala (img) VALUES (?)";
      $cordovaSQLite.execute(db, query, [img]).then(function(res) {
        window.alert("INSERT ID -> " + res.insertId);
      }, function (err) {
        window.alert(err);
      });
      window.alert("Picture Captured .. !!");
    }, function(err) {
      window.alert("Error hai baaaa..!!"+err);
    });
 }
    $scope.takePhoto=function(){
      var options = {
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
        encodingType: Camera.EncodingType.JPEG
      };
      $cordovaCamera.getPicture(options).then(function(imageData) {
        $scope.pictureUrl = "data:image/jpeg;base64," + imageData;
        window.alert("Picture Captured .. !!");
      }, function(err) {
        window.alert("Error hai baaaa..!!"+err.message);
      });
    }
    $scopr.selectPhoto= function () {
      var query = "select img from imagewala";
      $cordovaSQLite.execute(db, query, []).then(function(res) {
        window.alert("SELECT ID -> " + res.insertId);
        $scope.pictureUrl=res.readAsArrayBuffer();
        window.alert("ahahaha");
      }, function (err) {
        window.alert(err);
      });
      window.alert("Photo Captured .. !!");
    }

  });

Index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <script>
      window.location='./main.html';
    </script>
    </head>
  <body>
  </body>
</html>  

Main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <script src="lib/ionic/js/ionic.bundle.js"></script>
  <script src="lib/ionic/js/ng-cordova.js"></script>
  <script src="js/ng-cordova.min.js"></script>
  <script src="cordova.js"></script>
  <script src="js/app.js"></script>
</head>
<body ng-app="starter" ng-controller="CameraCtrl">
<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Camera</h1>
  </ion-header-bar>
  <ion-content >
    <div class="list card">
      <div class="item item-image">
        <img src="{{pictureUrl}}">
      </div>
    </div>
    <div class="padding">
      <button ng-click="takePicture(pictureUrl)" class="button button-block button-calm">Take Picture</button>
      <button ng-click="takePhoto()" class="button button-block button-positive">Choose Picture</button>
      <button ng-click="selectPhoto()" class="button button-block button-assertive">Choose Picture</button>

    </div>
  </ion-content>
</ion-pane>
</body>
</html>

據我所知,cordova s​​qlite插件不支持blob數據類型,請嘗試將數據類型img更改為TEXT數據類型並進行檢查。

我希望這可以解決您的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM