如何存储便于检索缩略图 [英] How to store thumbnails for easy retrieval

查看:119
本文介绍了如何存储便于检索缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 ThumbnailUtils.createVideoThumbnail()方法创建缩略图;它返回一个位图。不过,我想存储在数据库中的缩略图,所以我以后可以访问它,我没有让重新创建缩略图。我的问题是我应该怎么这个缩略图存储在数据库中?不要缩略图有文件路径?或者我应该创建缩略图,只是每次我需要使用它时使用Mediastore找回它们?如果是这样我将如何去保存/存储缩略图,所以我可以用Mediastore查询呢?

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?

感谢您的帮助。

推荐答案

如果你得到从视频缩略图的对象,你需要将其保存在任何存储或数据库。

If you're getting a Thumbnail object from video, you need to save it in either storage or 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;
}

数据库结构:

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

这篇关于如何存储便于检索缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆