1. There are two ways to get a video Thumbnail (1) through the content provider (2) create a thumbnail manually
(1) The disadvantage is that you must update the media library to see the thumbnail of the latest video.
[Java]
Copy codeThe Code is as follows :/**
* @ Param context
* @ Param cr
* @ Param Videopath
* @ Return
*/
Public static Bitmap getVideoThumbnail (Context context, ContentResolver cr, String Videopath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Video. Media. DATA, MediaStore. Video. Media. _ ID ,};
String whereClause = MediaStore. Video. Media. DATA + "= '" + Videopath + "'";
Cursor cursor = testcr. query (MediaStore. Video. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String videoPath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){
Int _ idColumn = cursor. getColumnIndex (MediaStore. Video. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Video. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
VideoPath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Video. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}
/**
* @ Param context
* @ Param cr
* @ Param Videopath
* @ Return
*/
Public static Bitmap getVideoThumbnail (Context context, ContentResolver cr, String Videopath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Video. Media. DATA, MediaStore. Video. Media. _ ID ,};
String whereClause = MediaStore. Video. Media. DATA + "= '" + Videopath + "'";
Cursor cursor = testcr. query (MediaStore. Video. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String videoPath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){
Int _ idColumn = cursor. getColumnIndex (MediaStore. Video. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Video. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
VideoPath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Video. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
} (2) it takes a little time to create a thumbnail manually.
[Java]Copy codeThe Code is as follows :/**
* Getting video thumbnails
* @ Param videoPath
* @ Param width
* @ Param height
* @ Param kind
* @ Return
*/
Private Bitmap getVideoThumbnail (String videoPath, int width, int height, int kind ){
Bitmap bitmap = null;
Bitmap = ThumbnailUtils. createVideoThumbnail (videoPath, kind );
Bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT );
Return bitmap;
}
/**
* Getting video thumbnails
* @ Param videoPath
* @ Param width
* @ Param height
* @ Param kind
* @ Return
*/
Private Bitmap getVideoThumbnail (String videoPath, int width, int height, int kind ){
Bitmap bitmap = null;
Bitmap = ThumbnailUtils. createVideoThumbnail (videoPath, kind );
Bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT );
Return bitmap;
}
2. Image thumbnails
[Java]
Copy codeThe Code is as follows :/**
*
* @ Param context
* @ Param cr
* @ Param Imagepath
* @ Return
*/
Public static Bitmap getImageThumbnail (Context context, ContentResolver cr, String Imagepath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Images. Media. DATA, MediaStore. Images. Media. _ ID ,};
String whereClause = MediaStore. Images. Media. DATA + "= '" + Imagepath + "'";
Cursor cursor = testcr. query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String imagePath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){
Int _ idColumn = cursor. getColumnIndex (MediaStore. Images. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Images. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
ImagePath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Images. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}
/**
*
* @ Param context
* @ Param cr
* @ Param Imagepath
* @ Return
*/
Public static Bitmap getImageThumbnail (Context context, ContentResolver cr, String Imagepath ){
ContentResolver testcr = context. getContentResolver ();
String [] projection = {MediaStore. Images. Media. DATA, MediaStore. Images. Media. _ ID ,};
String whereClause = MediaStore. Images. Media. DATA + "= '" + Imagepath + "'";
Cursor cursor = testcr. query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, projection, whereClause,
Null, null );
Int _ id = 0;
String imagePath = "";
If (cursor = null | cursor. getCount () = 0 ){
Return null;
}
If (cursor. moveToFirst ()){
Int _ idColumn = cursor. getColumnIndex (MediaStore. Images. Media. _ ID );
Int _ dataColumn = cursor. getColumnIndex (MediaStore. Images. Media. DATA );
Do {
_ Id = cursor. getInt (_ idColumn );
ImagePath = cursor. getString (_ dataColumn );
} While (cursor. moveToNext ());
}
Cursor. close ();
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inDither = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bitmap = MediaStore. Images. Thumbnails. getThumbnail (cr, _ id, Images. Thumbnails. MINI_KIND,
Options );
Return bitmap;
}