How to save thumbnails for easy search

I create sketches using the method ThumbnailUtils.createVideoThumbnail(); which returns a bitmap. However, I want to save this thumbnail in the database so that I can access it later, and I do not need to recreate the thumbnails. My questions: how to save this thumbnail in the database? Do thumbnails have file paths? Or should I create thumbnails and just retrieve them using Mediastore every time I need to use it? If so, how do I go about saving / saving the sketch so that I can use Mediastore to request it?

Thank you for your help.

0
source share
1 answer

Thumbnail , .

:

Bitmap thumbnailBitmap; // Get it with your approach
SQLiteDatabase writableDb; // Get it with your approach

if (thumbnailBitmap != null) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    thumbnailBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] thumbnailBitmapBytes = stream.toByteArray();

    ContentValues values = new ContentValues();
    values.put("IMAGEID", "your_image_id");
    values.put("BYTES", thumbnailBitmapBytes);
    writableDb.insert("TABLE_NAME", null, values);
}

:

public static synchronized Bitmap getImage(String imageID, Context context) {
    SQLiteDatabase writableDb; // Get it with your approach
    Bitmap bitmap = null;
    Cursor cs = null;

    try {
        String sql = "SELECT BYTES FROM TABLE_NAME WHERE IMAGEID = ?;";
        cs = writableDb.rawQuery(sql, new String[]{imageID});

        if (cs != null && cs.moveToFirst()) {
            do {
                byte[] bytes = cs.getBlob(0);

                if (bytes != null) {
                    try {
                        bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    } catch (Exception e) {
                        Log.e("TAG", "Exception", e);
                    }
                } else {
                    Log.e("TAG", "IMAGE NOT FOUND");
                }

            } while (cs.moveToNext());
        }

    } catch (Exception e) {
        Log.e("TAG", "Exception", e);
    } finally {
        if (cs != null) {
            cs.close();
        }
    }

    return bitmap;
}

:

String imageTable = "CREATE TABLE TABLE_NAME("
        + "IMAGEID TEXT PRIMARY KEY, "
        + "BYTES BLOB)";
+2

All Articles