我目前正在使用ThumbnailUtils.createVideoThumbnail()
方法创建缩略图;它返回一个位图。但是,我想将该缩略图存储在数据库中,以便稍后可以访问它,而不必继续重新创建缩略图。我的问题是如何将这个缩略图存储在数据库中?缩略图是否有文件路径?或者我应该创建缩略图,并在每次需要使用它时使用Mediastore检索它们?如果是这样,我将如何保存/存储缩略图,以便我可以使用Mediastore进行查询?
感谢您的帮助。
答案 0 :(得分:2)
如果您从视频中获取缩略图对象,则需要将其保存在存储或数据库中。
保存在数据库中:
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)";