sqlite异常已被调用的目标抛出

时间:2020-03-07 15:25:49

标签: c# xamarin.forms

我遇到了一些问题。我已经连接了SQLite,并且尝试添加一些数据,这些数据一直有效,直到我决定也要在其中添加摄像机图片,现在它引发了异常。我正在开发移动应用程序,在该应用程序中,用户应该通过拍照来创建帖子

这是我在Android中的SQL

[assembly: Dependency(typeof(SQLiteDb))]
namespace Baltazar.Droid
{
    public class SQLiteDb : ISQLiteInterface
    {

        public SQLiteConnection GetSQLiteConnection()
        {
            var fileName = "Mydatabase.db3";
            string dbpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(dbpath, fileName);
            var conn = new SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache);
            // Return the database connection 
            return conn;
        }

    }
}

我在共享项目中的界面

公共接口ISQLiteInterface {

    SQLiteConnection GetSQLiteConnection();

}

“我的添加”页面一旦我不想初始化它就会引发期望

using System;
using System.Threading.Tasks;
using SQLite;
using Baltazar.Model;
using Plugin.Media;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Plugin.Media.Abstractions;
using System.IO;
using Baltazar.Resources;
using Baltazar.ViewModel;
using System.Collections.ObjectModel;
using Baltazar.Service;

namespace Baltazar.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AddingPage : ContentPage
    {
        private SQLiteConnection _conn;
        AdLogEntry adLogEntry = new AdLogEntry();
        public ObservableCollection<AdLogEntry> AdLogEntries = new ObservableCollection<AdLogEntry>();
        public ObservableCollection<PictureMedia> PictureMedias  = new ObservableCollection<PictureMedia>();

        private AdService _adService;

        public AddingPage()
        {
            InitializeComponent();
            _adService = new AdService();
            BindingContext = new AnimalCategoryViewModel();
            _conn = DependencyService.Get<Helpers.ISQLiteInterface>().GetSQLiteConnection();
            _conn.CreateTable<AdLogEntry>();

        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();

            await MainProgressBar.ProgressTo(0, 250, Easing.Linear); ;
        }
        private async void TakePicture_Clicked(object sender, EventArgs e)
        {
            await TakePicture();

        }

        //private async void TakeVideo_Clicked(object sender, EventArgs e)
        //{
        //    await TakeVideo();
        //}

        private async Task TakePicture()
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) //is or is not supported
            {
                await DisplayAlert("No access", "no camra", "ok");
                return;
            }
            var imageFilename = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions()
            {
                Name = $"{DateTime.UtcNow}.jpg",
                DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Rear,
                PhotoSize = PhotoSize.Medium,
                SaveToAlbum = true
            });
            if (imageFilename == null) return;
            byte[] imageAsBytes = ConvertImageToBytes(imageFilename);
            var imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
            AdLogEntries.Add(new AdLogEntry { ImagePath = imageFilename.AlbumPath, ImageName = imageSource });

            PhotoListView.ItemsSource = AdLogEntries;

            //await GridPhoto();
        }
        private byte[] ConvertImageToBytes(MediaFile imageFilename)
        {
            byte[] imageAsBytes = null;
            using (var memoryStream = new MemoryStream())
            {
                imageFilename.GetStream().CopyTo(memoryStream);
                imageAsBytes = memoryStream.ToArray();
            }
            return imageAsBytes;
        }

        private async void PickPicture_Clicked(object sender, EventArgs e)
        {
            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Not supported", "", "ok");
                return;
            }
            var imageFilename = await CrossMedia.Current.PickPhotoAsync();
            if (imageFilename != null)
            {
              Photo.Source = ImageSource.FromFile(imageFilename.Path);
            }

        }
        private async void NextStep_Clicked(object sender, EventArgs e)
        {
            await SaveAdLog();

        }
        //TODO IsNullOR for all
        //TODO Marks for required fields
        private async Task SaveAdLog()
        {
            if (!string.IsNullOrWhiteSpace(NameEntry.Text) || (!string.IsNullOrWhiteSpace(PriceEntry.Text) || (!string.IsNullOrWhiteSpace(LocationEntry.Text))))

            {
                AdLogEntry adLogEntry = new AdLogEntry
                {
                    Location = LocationEntry.Text,
                    Price = PriceEntry.Text,
                    Name = NameEntry.Text,


                };
                _adService.CreateAddLogEntry(adLogEntry);
                await DisplayAlert(LabelCZ.AlertThankYou, LabelCZ.AlertSpace, LabelCZ.AlertThankYou);
            }
            else
            {
                await DisplayAlert(LabelCZ.AlertRequired, LabelCZ.AlertRequiredPlease, LabelCZ.AlertOk);
            };

        }

}

广告服务

public class AdService
    {
        private SQLiteConnection _conn;

        public AdService()
        {
           _conn = DependencyService.Get<Helpers.ISQLiteInterface>().GetSQLiteConnection();
           _conn.CreateTable<AdLogEntry>();

        }
        public bool CreateAddLogEntry(AdLogEntry adLogEntry)
        {
            try
            {
                using (_conn = DependencyService.Get<Helpers.ISQLiteInterface>().GetSQLiteConnection())
                {
                    _conn.Insert(adLogEntry);
                    return true;
                }
            }
            catch (SQLiteException ex)
            {
                Log.Info("SQLiteEx", ex.Message);
                return false;
            }
        }

My model

  public class AdLogEntry
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string SellerName { get; set; }
        public string ImagePath { get; set; }
        public ImageSource ImageName { get; set; }
        public byte[] OriginalImage { get; set; }
        public string Info { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
        public string  LoadedDate { get; set; }
        public string Location { get; set; }
        public int Age { get; set; }
    }


public class HomePageViewModel : BaseViewModel

    {

        private ObservableCollection<AdLogEntry> _adLogEntries;
        public ObservableCollection<AdLogEntry> AdLogEntries { get => _adLogEntries; set { _adLogEntries = value; OnPropertyChanged(); } }
      }

我确信我所做的不只是做错事情或违反一些通用规则,但我一直在努力使这项工作持续一周左右,所以我感到绝望

然后输出

03-07 16:21:48.228 D/Mono    (10005): Loading reference 1 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for SQLitePCLRaw.core, Version=1.1.13.388, Culture=neutral, PublicKeyToken=1488e028ca7ab535
03-07 16:21:48.228 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> SQLitePCLRaw.core[0x7b5381a6fe00]: 2
03-07 16:21:48.399 D/Mono    (10005): Loading reference 6 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/Baltazar.Android.dll asmctx DEFAULT, looking for SQLite-net, Version=1.6.292.0, Culture=neutral, PublicKeyToken=null
03-07 16:21:48.400 D/Mono    (10005): Assembly Ref addref Baltazar.Android[0x7b536e3d7380] -> SQLite-net[0x7b5381a6f900]: 4
03-07 16:21:48.408 D/Mono    (10005): Loading reference 9 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for SQLitePCLRaw.batteries_v2, Version=1.1.13.388, Culture=neutral, PublicKeyToken=8226ea5df37bcae9
03-07 16:21:48.410 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> SQLitePCLRaw.batteries_v2[0x7b5381a6fd00]: 2
03-07 16:21:48.413 D/Mono    (10005): Loading reference 1 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLitePCLRaw.batteries_v2.dll asmctx DEFAULT, looking for SQLitePCLRaw.provider.e_sqlite3, Version=1.1.13.388, Culture=neutral, PublicKeyToken=9c301db686d0bd12
03-07 16:21:48.413 D/Mono    (10005): Assembly Ref addref SQLitePCLRaw.batteries_v2[0x7b5381a6fd00] -> SQLitePCLRaw.provider.e_sqlite3[0x7b5381a4c280]: 2
03-07 16:21:48.413 D/Mono    (10005): Loading reference 1 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLitePCLRaw.provider.e_sqlite3.dll asmctx DEFAULT, looking for SQLitePCLRaw.core, Version=1.1.13.388, Culture=neutral, PublicKeyToken=1488e028ca7ab535
03-07 16:21:48.413 D/Mono    (10005): Assembly Ref addref SQLitePCLRaw.provider.e_sqlite3[0x7b5381a4c280] -> SQLitePCLRaw.core[0x7b5381a6fe00]: 3
03-07 16:21:48.416 D/Mono    (10005): Loading reference 2 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLitePCLRaw.batteries_v2.dll asmctx DEFAULT, looking for SQLitePCLRaw.core, Version=1.1.13.388, Culture=neutral, PublicKeyToken=1488e028ca7ab535
03-07 16:21:48.416 D/Mono    (10005): Assembly Ref addref SQLitePCLRaw.batteries_v2[0x7b5381a6fd00] -> SQLitePCLRaw.core[0x7b5381a6fe00]: 4
03-07 16:21:48.427 D/Mono    (10005): DllImport attempting to load: 'e_sqlite3'.
03-07 16:21:48.431 D/Mono    (10005): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/e_sqlite3': '(null)'.
03-07 16:21:48.435 D/Mono    (10005): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/libe_sqlite3.so': '(null)'.
03-07 16:21:48.438 D/Mono    (10005): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/libe_sqlite3.so': '(null)'.
03-07 16:21:48.438 D/Mono    (10005): DllImport error loading library '/system/lib64/e_sqlite3': '(null)'.
03-07 16:21:48.438 D/Mono    (10005): DllImport error loading library '/system/lib64/libe_sqlite3.so': '(null)'.
03-07 16:21:48.438 D/Mono    (10005): DllImport error loading library '/system/lib64/libe_sqlite3.so': '(null)'.
03-07 16:21:48.443 D/Mono    (10005): DllImport error loading library 'e_sqlite3': 'dlopen failed: library "e_sqlite3" not found'.
03-07 16:21:48.444 D/Mono    (10005): DllImport loaded library 'libe_sqlite3.so'.
03-07 16:21:48.444 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.444 D/Mono    (10005): Searching for 'sqlite3_libversion_number'.
03-07 16:21:48.472 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.472 D/Mono    (10005): Searching for 'sqlite3_open_v2'.
03-07 16:21:48.503 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.503 D/Mono    (10005): Searching for 'sqlite3_busy_timeout'.
03-07 16:21:48.513 D/Mono    (10005): Loading reference 8 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
03-07 16:21:48.515 D/Mono    (10005): Image addref System.Linq[0x7b536c652100] (asmctx DEFAULT) -> System.Linq.dll[0x7b5381bad000]: 2
03-07 16:21:48.515 D/Mono    (10005): Prepared to set up assembly 'System.Linq' (System.Linq.dll)
03-07 16:21:48.515 D/Mono    (10005): Assembly System.Linq[0x7b536c652100] added to domain RootDomain, ref_count=1
03-07 16:21:48.517 D/Mono    (10005): AOT: image 'System.Linq.dll.so' not found: dlopen failed: library "System.Linq.dll.so" not found
03-07 16:21:48.518 D/Mono    (10005): AOT: image '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/lib/mono/aot-cache/amd64/System.Linq.dll.so' not found: (null)
03-07 16:21:48.518 D/Mono    (10005): Config attempting to parse: 'System.Linq.dll.config'.
03-07 16:21:48.518 D/Mono    (10005): Config attempting to parse: '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/etc/mono/assemblies/System.Linq/System.Linq.config'.
03-07 16:21:48.518 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> System.Linq[0x7b536c652100]: 2
03-07 16:21:48.518 D/Mono    (10005): Loading reference 1 of System.Linq.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-07 16:21:48.518 D/Mono    (10005): Assembly Ref addref System.Linq[0x7b536c652100] -> System.Core[0x7b536e2d8000]: 7
03-07 16:21:48.519 D/Mono    (10005): Loading reference 5 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for System.Reflection, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
03-07 16:21:48.520 D/Mono    (10005): Unloading image System.Reflection.dll [0x7b536d4e0000].
03-07 16:21:48.521 D/Mono    (10005): Image addref System.Reflection[0x7b536c652480] (asmctx DEFAULT) -> System.Reflection.dll[0x7b536dcd5000]: 5
03-07 16:21:48.521 D/Mono    (10005): Config attempting to parse: 'System.Reflection.dll.config'.
03-07 16:21:48.521 D/Mono    (10005): Config attempting to parse: '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/etc/mono/assemblies/System.Reflection/System.Reflection.config'.
03-07 16:21:48.521 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> System.Reflection[0x7b536dc59300]: 3
Loaded assembly: System.Linq.dll [External]
03-07 16:21:48.536 D/Mono    (10005): Loading reference 10 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for System.Threading, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
03-07 16:21:48.537 D/Mono    (10005): Image addref System.Threading[0x7b536c652500] (asmctx DEFAULT) -> System.Threading.dll[0x7b536d4fd000]: 2
03-07 16:21:48.538 D/Mono    (10005): Prepared to set up assembly 'System.Threading' (System.Threading.dll)
03-07 16:21:48.538 D/Mono    (10005): Assembly System.Threading[0x7b536c652500] added to domain RootDomain, ref_count=1
03-07 16:21:48.539 D/Mono    (10005): AOT: image 'System.Threading.dll.so' not found: dlopen failed: library "System.Threading.dll.so" not found
03-07 16:21:48.540 D/Mono    (10005): AOT: image '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/lib/mono/aot-cache/amd64/System.Threading.dll.so' not found: (null)
03-07 16:21:48.540 D/Mono    (10005): Config attempting to parse: 'System.Threading.dll.config'.
03-07 16:21:48.541 D/Mono    (10005): Config attempting to parse: '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/etc/mono/assemblies/System.Threading/System.Threading.config'.Loaded assembly: System.Threading.dll [External]

03-07 16:21:48.541 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> System.Threading[0x7b536c652500]: 2
03-07 16:21:48.541 D/Mono    (10005): Loading reference 0 of System.Threading.dll asmctx DEFAULT, looking for mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-07 16:21:48.541 D/Mono    (10005): Assembly Ref addref System.Threading[0x7b536c652500] -> mscorlib[0x7b538621e080]: 71
03-07 16:21:48.566 D/Mono    (10005): Loading reference 1 of System.Collections.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-07 16:21:48.566 D/Mono    (10005): Assembly Ref addref System.Collections[0x7b536dc59180] -> System.Core[0x7b536e2d8000]: 8
03-07 16:21:48.570 D/Mono    (10005): Loading reference 11 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for System.Reflection.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
03-07 16:21:48.570 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> System.Reflection.Extensions[0x7b536dc59580]: 3
03-07 16:21:48.738 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.738 D/Mono    (10005): Searching for 'sqlite3_prepare_v2'.
03-07 16:21:48.768 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.769 D/Mono    (10005): Searching for 'sqlite3_column_count'.
03-07 16:21:48.772 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.772 D/Mono    (10005): Searching for 'sqlite3_column_name'.
03-07 16:21:48.784 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.784 D/Mono    (10005): Searching for 'sqlite3_step'.
03-07 16:21:48.793 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.793 D/Mono    (10005): Searching for 'sqlite3_column_type'.
03-07 16:21:48.795 D/Mono    (10005): Loading reference 12 of /storage/emulated/0/Android/data/com.companyname.baltazar/files/.__override__/SQLite-net.dll asmctx DEFAULT, looking for System.Globalization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
03-07 16:21:48.797 D/Mono    (10005): Image addref System.Globalization[0x7b536c652700] (asmctx DEFAULT) -> System.Globalization.dll[0x7b536c73a000]: 2
03-07 16:21:48.797 D/Mono    (10005): Prepared to set up assembly 'System.Globalization' (System.Globalization.dll)
03-07 16:21:48.797 D/Mono    (10005): Assembly System.Globalization[0x7b536c652700] added to domain RootDomain, ref_count=1
03-07 16:21:48.799 D/Mono    (10005): AOT: image 'System.Globalization.dll.so' not found: dlopen failed: library "System.Globalization.dll.so" not found
03-07 16:21:48.799 D/Mono    (10005): AOT: image '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/lib/mono/aot-cache/amd64/System.Globalization.dll.so' not found: (null)
03-07 16:21:48.800 D/Mono    (10005): Config attempting to parse: 'System.Globalization.dll.config'.
03-07 16:21:48.800 D/Mono    (10005): Config attempting to parse: '/Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/sdks/out/android-x86_64-release/etc/mono/assemblies/System.Globalization/System.Globalization.config'.
03-07 16:21:48.800 D/Mono    (10005): Assembly Ref addref SQLite-net[0x7b5381a6f900] -> System.Globalization[0x7b536c652700]: 2
03-07 16:21:48.800 D/Mono    (10005): Loading reference 0 of System.Globalization.dll asmctx DEFAULT, looking for mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-07 16:21:48.800 D/Mono    (10005): Assembly Ref addref System.Globalization[0x7b536c652700] -> mscorlib[0x7b538621e080]: 72
03-07 16:21:48.801 D/Mono    (10005): Loading reference 2 of System.Runtime.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-07 16:21:48.802 D/Mono    (10005): Assembly Ref addref System.Runtime[0x7b536dc2bd80] -> System[0x7b5381abb800]: 11
Loaded assembly: System.Globalization.dll [External]
03-07 16:21:48.817 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.817 D/Mono    (10005): Searching for 'sqlite3_column_text'.
03-07 16:21:48.827 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.827 D/Mono    (10005): Searching for 'sqlite3_column_int'.
03-07 16:21:48.838 D/Mono    (10005): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
03-07 16:21:48.839 D/Mono    (10005): Searching for 'sqlite3_finalize'.
**System.Reflection.TargetInvocationException:** 'Exception has been thrown by the target of an invocation.

'

1 个答案:

答案 0 :(得分:2)

SQLite不知道ImageSource是什么。如果要将图像存储在数据库中,请改为将其文件路径存储为string

# change this
public ImageSource ImageName { get; set; }
# to this
public string ImagePath { get; set; }