0

I'm currently creating thumbnails by using the ThumbnailUtils.createVideoThumbnail() method; which returns a bitmap. However, I want to store that thumbnail in a database so I can access it later and I don't have to keep recreating the thumbnails. My questions is how should I store this thumbnail in the database? Do thumbnails have filepaths? Or should I create the thumbnails and just retrieve them using the Mediastore every time I need to use it? If so how would I go about saving/storing the thumbnail so I can use the Mediastore to query it?

Thanks for your help.

4

1 回答 1

2

If you're getting a Thumbnail object from video, you need to save it in either storage or database.

To save in database :

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);
}

To get it back from database :

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;
}

The database structure:

String imageTable = "CREATE TABLE TABLE_NAME("
        + "IMAGEID TEXT PRIMARY KEY, "
        + "BYTES BLOB)";
于 2013-06-05T03:54:36.520 回答