2013-06-05 46 views
0

我目前通過使用ThumbnailUtils.createVideoThumbnail()方法創建縮略圖;它返回一個位圖。但是,我想將縮略圖存儲在數據庫中,以便以後可以訪問它,並且不必重新創建縮略圖。我的問題是我應該如何將這個縮​​略圖存儲在數據庫中?縮略圖是否有文件路徑?或者我應該創建縮略圖,並在每次需要使用時使用Mediastore檢索它們?如果是這樣,我將如何去保存/存儲縮略圖,以便我可以使用Mediastore來查詢它?如何存儲縮略圖以方便檢索

感謝您的幫助。

回答

2

如果您從視頻獲取縮略圖對象,則需要將其保存在存儲或數據庫中。

要保存在數據庫:

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

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

從數據庫中找回來