Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Saving image in SQLite database

Xammy10Xammy10 Member ✭✭✭

Is is there a way to save a image on SQLite database? All of the tutorials I've search so far are only available in android studio/java. I need a c# sample of saving image on SQLite database. Can someone give some sample link or teach me how to do it? Thanks in advance :)

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @enohara,
    for me , you need to try to convert the sample in java in c# that's a good exercice for you and for the futur you'll see often when we meet a problem you'll find a solution perhpas in java ...
    it's my opinion ...

    now, nothing found about this ...but try the sample in java and if you meet some problem post it here ...
    i will help you ..

  • Xammy10Xammy10 Member ✭✭✭

    @ChristopheBERNARD thanks for the advice sir,
    I already get the flow of saving on JAVA/Android studio.

    I need to convert my image to byte array / base64, then save it to my SQLite database.
    and when it comes to retrieving, I'll get the Base64string file android convert it to bitmap.

    But I can't find a codes for android xamarin when it comes to converting android retrieving.
    I tried searching but most of the results are also Java/android studio.

    I found some in converting but most of it are in C# (Windows form) and I can't convert it to xamarin android.

    Here Sir:
    https://stackoverflow.com/questions/18827081/c-sharp-base64-string-to-jpeg-image
    https://stackoverflow.com/questions/36973960/convert-base64-string-to-bitmap-or-image-xamarin-or-android

    I already know the procedure, but I still don't know the codes.

    Hope you help me with this Sir. Thanks Again :)

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Try this and insert it into sql lite

    `

    public static string ConvertBitmapToString(Bitmap theBitmap)
    {
    string strImage = string.Empty;
    using (var stream = new System.IO.MemoryStream())
    {
    theBitmap.Compress(Bitmap.CompressFormat.Png, 25, stream);
    var bytes = stream.ToArray();
    strImage = Convert.ToBase64String(bytes);
    }

            return strImage;
        }
    
        public static Bitmap ConvertStringToBitmap(string theBitmap)
        {
            Bitmap img = null;
    
            if (theBitmap != null)
            {
                byte[] decodedByte = Android.Util.Base64.Decode(theBitmap, 0);
                img = BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length);
    
            }
    
            return img;
        }
    

    `

  • Xammy10Xammy10 Member ✭✭✭
    edited February 2018

    @ChristopheBERNARD said:
    Try this and insert it into sql lite

    `

    public static string ConvertBitmapToString(Bitmap theBitmap)
    {
    string strImage = string.Empty;
    using (var stream = new System.IO.MemoryStream())
    {
    theBitmap.Compress(Bitmap.CompressFormat.Png, 25, stream);
    var bytes = stream.ToArray();
    strImage = Convert.ToBase64String(bytes);
    }

            return strImage;
        }
    
        public static Bitmap ConvertStringToBitmap(string theBitmap)
        {
            Bitmap img = null;
    
            if (theBitmap != null)
            {
                byte[] decodedByte = Android.Util.Base64.Decode(theBitmap, 0);
                img = BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length);
    
            }
    
            return img;
        }
    

    `

    I'll give it a try Sir, thank you for this.
    I'll inform you if it's working :)))))

  • terzyterzy Member ✭✭

    @Enohara
    You havent give a update on this.
    I have the same problem have searched for days and cant find a good solution

  • terzyterzy Member ✭✭

    And one part more.

    When you have the method

    how do i but the image into the method, for me it just say i have inserted a integer

    ConvertBitmapToString(Resorce.drawing.item)

  • DimChrisDimChris USMember ✭✭✭✭

    If you have to handle big images, it is better(i think...) to save your images inside mobile storage. And when you need your image, retreive it from a storage path.

    Example

    First step - Save image name in sqlite(or full path) then save your image to this path
    Second step - Select image name(or full path) from sqlite, and show it inside an imageview...

Sign In or Register to comment.