How to store thumbnails for easy retrieval

为君一笑 提交于 2019-12-25 01:54:08

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/16930681/how-to-store-thumbnails-for-easy-retrieval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!