Xamarin.android 使用Sqlite捕获、保存和检索图像/图片

Xamarin.android 使用Sqlite捕获、保存和检索图像/图片,xamarin.android,Xamarin.android,如何将图像从imageview保存到sqlite数据库,并再次检索到Imgeview 根据您的描述,您希望将ImageView图像保存到Sqlite数据库中,并将图像从Sqlite数据库加载到ImageView中,对吗 如果是,首先需要创建类来加载图像: public class Image { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string FileName { get; set

如何将图像从imageview保存到sqlite数据库,并再次检索到Imgeview


根据您的描述,您希望将ImageView图像保存到Sqlite数据库中,并将图像从Sqlite数据库加载到ImageView中,对吗

如果是,首先需要创建类来加载图像:

public class Image
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }
}
然后安装sqlite net pcl以连接sqlite数据库

最后,您可能需要考虑将IVIEVIEW图像转换为字节[]以保存,并将字节[]转换成BITMP到IVIEVIEW中的加载。 请查看完整代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical">

<ImageView android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:src="@drawable/a11"/>

<Button android:id="@+id/button1" 
    android:text="insert image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ImageView android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<Button android:id="@+id/button2" 
    android:text="display image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/button3" 
    android:text="create table"/>
</LinearLayout>

 public class MainActivity : AppCompatActivity
{
    private ImageView imageview1;
    private ImageView imageview2;

    private byte[] insertbitmapData;

    private Button btninsert;
    private Button btnload;
    private Button btntable;

    private SQLiteConnection _SQLiteConnection;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);


        btninsert = FindViewById<Button>(Resource.Id.button1);
        btninsert.Click += Btninsert_Click;
        btnload = FindViewById<Button>(Resource.Id.button2);
        btnload.Click += Btnload_Click;
        btntable = FindViewById<Button>(Resource.Id.button3);
        btntable.Click += Btntable_Click;
        imageview1 = FindViewById<ImageView>(Resource.Id.imageView1);
        imageview2 = FindViewById<ImageView>(Resource.Id.imageView2);

    }

    private void Btnload_Click(object sender, System.EventArgs e)
    {
        string filename = "test";
        var imagedata = _SQLiteConnection.Table<Image>();
        var d1 = imagedata.Where(x => x.FileName == filename).FirstOrDefault();
        if(d1!=null)
        {
            byte[] imageBytes = d1.Content;             
            using (var ms = new MemoryStream(imageBytes))
            {
                var bitmap = BitmapFactory.DecodeStream(ms);
                imageview2.SetImageBitmap(bitmap);

            }

        }
    }

    private void Btninsert_Click(object sender, System.EventArgs e)
    {
        Bitmap bmp = ((BitmapDrawable)imageview1.Drawable).Bitmap;

        using (var stream = new MemoryStream())
        {
            bmp.Compress(Bitmap.CompressFormat.Png, 0, stream);
            insertbitmapData = stream.ToArray();
        }
        Image image = new Image();
        image.FileName = "test";
        image.Content = insertbitmapData;

        var imagedata = _SQLiteConnection.Table<Image>();

        var d1 = imagedata.Where(x => x.FileName == image.FileName).FirstOrDefault();
        if (d1 == null)
        {
            _SQLiteConnection.Insert(d1);
            Console.WriteLine("Sucessfully Added");
        }
        else
        {
            Console.WriteLine("Already image id Exist");
        }            
    }

    private void Btntable_Click(object sender, System.EventArgs e)
    {
        createsqlite();
    }

    private void createsqlite()
    {
        var fileName = "imagedata.db3";
        var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = System.IO.Path.Combine(documentPath, fileName);

        _SQLiteConnection = new SQLiteConnection(path);
        _SQLiteConnection.CreateTable<Image>();
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

public class Image
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }
}

hi cherry当您从数据库中单击“还原并上载图像”按钮时,什么都没有appears@maher2020首先,您需要创建一个sqlite数据库,方法是单击create table按钮,然后单击Button1将imageview图像插入sqlite数据库,最后,您可以通过单击按钮2将图像从sqlite数据库获取到imageview2中。您好,谢谢您的关注。我认为我没有错过创建表、输入数据和显示数据的工作步骤。我对链接做了一些更改。问题remains@maher2020现在我可以在测试我的代码时重现您的问题,但它昨天运行良好,所以我建议您可以创建新类并创建新表以重试。看看我的更新。
public class User
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string userName { get; set; }
    public string password { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }


}