How to implement SQLite database to store Bitmap Image and Text?

后端 未结 3 615
忘了有多久
忘了有多久 2021-01-28 00:11

hello all i am developing an android application which listen to incoming whatsapp notification and show it in listView using NotificationListenerService. i need he

相关标签:
3条回答
  • 2021-01-28 00:37

    If your image is really small you can covert it in a String by means of android.util.Base64 encoding and put this string in SQLite database:

    public static String getPngAsString(Bitmap bitmap){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bitmapBytes = bos.toByteArray();
        return Base64.encodeToString(bitmapBytes, Base64.NO_WRAP);
    }
    
    0 讨论(0)
  • 2021-01-28 00:41

    Storing Bitmap images in a SQL is actually a bad idea.

    My suggestion is to save you images online and save the URL link in the database.

    You can take the URL from the database and show it in your app using Picasso (link).

    Also, remember to ask for internet permission in your app to load images from a url.

    0 讨论(0)
  • 2021-01-28 01:01

    Create a class by name BitmapBase64.class and use whereever you need. Either way of conversion can be done.

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.Base64;
    import java.io.ByteArrayOutputStream;
    
    public class BitmapBase64
    {
        public static Bitmap convert(String base64Str) throws IllegalArgumentException
        {
            byte[] decodedBytes = Base64.decode(
                base64Str.substring(base64Str.indexOf(",")  + 1),
                Base64.DEFAULT
            );
            return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
        }
    
        public static String convert(Bitmap bitmap)
        {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);    
            return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
        }    
    }
    

    Usage :

    Bitmap bitmap = BitmapBase64.convert(BASE_64_STRING);
    
    String base64String = BitmapBase64.convert(BITMAP);
    

    But this is suggested if the image is small, if not go with online storage and retrieval. Be careful with out of memory as well as this is quite often when your dealing with bitmaps.

    0 讨论(0)
提交回复
热议问题